我试图从使用 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.