0

我试图从使用 Delphi XE2 的网站解析 JSON 并遵循此线程中建议的代码。当我完全按照受访者帖子中的代码实现代码时,它可以工作,但是当我在我的代码中实现几乎相同的东西时,我在一行中遇到访问冲突:

LParts:=LJsonObj.Get('parts').JsonValue;

并且每次都使用 AV 失败(运行时)(编译正常,只是无法运行)。我已经简化了我的代码,下面是我希望工作的片段。毕竟,这是其他工作代码的复制和粘贴。

如果有人可以在下面查看并让我知道为什么我的不起作用但引用帖子中的代码可以,我将非常感激。我很困惑,主要是因为当我调试它时,它看起来应该可以工作并且几乎没有调试信息可以关闭。

program ReadJSONConsoleApp;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  DBXJSON,
  System.SysUtils;


Const
StrJson=
'{' +
'  "response" : [ {' +
'    "distributor" : {' +
'      "id" : 1538,' +
'      "name" : "Arrow Electronics",' +
'      "authorized" : true,' +
'      "logoUrl" : "this is normally a URL but I cut it out"' +
'    },' +
'    "parts" : [ {' +
'      "manufacturer" : "National Semiconductor",' +
'      "part" : "LM741WG/883",' +
'      "description" : "OP Amp Single GP ±22V 10-Pin CFPAK Tray",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 65.0,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 88,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "National Semiconductor",' +
'      "part" : "LM741W/883",' +
'      "description" : "OP Amp Single GP ±22V 10-Pin CPAK Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 40.5,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 1464,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CH",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 95,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.3633,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4320,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 3458,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.71,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 0,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN/NOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.2977,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 6486,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 7.21,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 362,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/NOPB",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 1378,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 11.8,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 989,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Tray",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 15.74,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4252,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CHNOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 785,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }' +
'    } ]' +
'  } ]' +
'}';

procedure ParseJson;
var
  LJsonObj  : TJSONObject;
  LJPair    : TJSONPair;
  LParts    : TJSONValue;
  LPart     : TJSONValue;
  LItem     : TJSONValue;
  LIndex    : Integer;
  LSize     : Integer;
begin
    LJsonObj    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
  try
     LParts:=LJsonObj.Get('parts').JsonValue;
     LSize:=TJSONArray(LParts).Size;
     for LIndex:=0 to LSize-1 do
     begin
      LPart := TJSONArray(LParts).Get(LIndex);
      LJPair   := TJSONPair(LPart);
      Writeln(Format('Part Name %s',[LJPair.JsonString.Value]));
        for LItem in TJSONArray(LJPair.JsonValue) do
        begin
           if TJSONPair(LItem).JsonValue is TJSONFalse then
            Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'false']))
           else
           if TJSONPair(LItem).JsonValue is TJSONTrue then
            Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'true']))
           else
            Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]));
        end;
     end;
  finally
     LJsonObj.Free;
  end;
end;

begin
  try
    ParseJson;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
4

1 回答 1

1

调用ParseJSONValue返回 nil。文档指出了这一点:

ParseJSONValue 返回解析后的数据对应的 JSON 值,解析失败则返回 null。

您没有检查这种情况,访问冲突是不可避免的后果。除非您的 JSON 已预先验证,否则您需要检查其有效性。

您可以通过将 JSON 粘贴到在线验证器中来自己仔细检查。例如: http: //jsonlint.com/执行此操作,您将看到 JSON 无效。

这是一些有效的 JSON:

StrJson=
'{' +
'  "response" : {' +
'    "distributor" : {' +
'      "id" : 1538,' +
'      "name" : "Arrow Electronics",' +
'      "authorized" : true,' +
'      "logoUrl" : "this is normally a URL but I cut it out"' +
'    },' +
'    "parts" : [ {' +
'      "manufacturer" : "National Semiconductor",' +
'      "part" : "LM741WG/883",' +
'      "description" : "OP Amp Single GP ±22V 10-Pin CFPAK Tray",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 65.0,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 88,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "National Semiconductor",' +
'      "part" : "LM741W/883",' +
'      "description" : "OP Amp Single GP ±22V 10-Pin CPAK Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 40.5,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 1464,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CH",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 95,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.3633,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4320,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 3458,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.71,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 0,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN/NOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.2977,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 6486,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 7.21,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 362,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/NOPB",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 1378,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 11.8,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 989,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Tray",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 15.74,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4252,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CHNOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 785,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }' +
'    ]' +
'  }' +
'}';

不管它是不是你想要的,我无法确定。

将此常量插入您的程序,它会进一步前进,但随后会失败Get('parts')nil当您尝试读取该JsonValue属性时,这也会返回并且您会得到另一个 AV。

不知道你在做什么,我不能告诉你如何调试整个代码。您需要做的是开始注意这些 JSON 方法和属性可能会返回的事实nil。因此,在您尝试访问nil引用上的方法和属性之前,请检查这一点。

我的另一条建议是将 JSON 放入一个文件中,以便更容易使用。像这样构建常量会使调试变得非常棘手。

于 2013-11-04T22:25:25.163 回答