Delphi XE2 - 解析JSON使用.Get('xyz')生成访问冲突。运行时JSonValue

时间:2013-11-04 21:29:44

标签: json delphi parsing

我试图使用Delphi XE2从网站解析JSON并遵循this thread here中建议的代码。当我完全按照在受访者帖子中显示的那样实现代码时,它可以工作,但是当我在我的实现几乎完全相同的内容时,我会在以下行中获得访问冲突:

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.

1 个答案:

答案 0 :(得分:1)

ParseJSONValue的调用返回nil。 documentation称之为:

  

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放入文件中,以便更容易使用。构建这样的常量会使调试变得非常棘手。