【问题标题】:Delphi. Parse JSON德尔福。解析 JSON
【发布时间】:2014-11-01 21:33:51
【问题描述】:

我正在尝试解析 JSON 字符串 (http://pastebin.com/zXn8pwtL):

sListing 包含 JSON 字符串

jsObj: TJSONObject;
jsItem, jsElem, jsAsserts: TJSONValue;
...
jsObj := TJSONObject.ParseJSONValue(sListing) as TJSONObject;
jsAsserts := TJSONObject(TJSONObject(jsObj.Get('assets').JsonValue).Get('730').JsonValue).Get('2').JsonValue;

for jsElem in TJSONArray(jsAsserts) do
  WriteLn(jsElem.ToString);

如何从 jsElem->descriptions 中获取所有值?我试图枚举所有:

for jsItem in TJSONArray(jsElem) do
  WriteLn(jsItem.ToString);

但是得到了EAccessViolation Read of address 00000001

【问题讨论】:

  • 检查强制转换是否有效。使用as 而不是未经检查的强制转换。
  • 您在没有数组的assets/730/2 路径中。数组是如果您深入了解特定对象,例如652761226 包含 descriptions 数组。
  • @DavidHeffernan 谢谢,但如果我使用 as 编译器会给我一个错误:Invalid class typecast
  • 是的,因为没有数组。这里是your JSON simplified
  • 嗯,正确的回应是接受编译器告诉你的!而不是决定你知道得更好并且无论如何都插上去。如果经过检查的演员表不起作用,那么未经检查的演员表怎么可能?

标签: json delphi


【解决方案1】:

好的。谢谢大家。这是对 JSON 类型的一些误解。现在已修复:

jsAsserts: TJSONObject;
jsDesc: TJSONArray;
iCurEl: integer;
jsItem: TJSONPair;
...
jsAsserts := TJSONObject(TJSONObject(jsObj.Get('assets').JsonValue).Get('730').JsonValue).Get('2').JsonValue as TJSONObject;
for iCurEl := 0 to jsAsserts.Count - 1 do
  begin
    jsItem := jsAsserts.Pairs[iCurEl];
    jsDesc := (jsItem.JsonValue as TJSONObject).Get('descriptions').JsonValue as TJSONArray;
    WriteLn(jsDesc.ToString);
  end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2011-10-04
    相关资源
    最近更新 更多