json - 获取xsuperobject将未命名的数组解析为tobject结构

标签 json delphi delphi-10.1-berlin

我正在使用Delphi 10.1 Berlin Update 2,并尝试使用XSuperObject / XSuperJSON从第三方提供程序获取JSON响应并将其解析为对象结构。

这是JSON:

[ 
  {
    "yardNumber": 10,
    "links": [
      {
        "rel": "yardSaleList",
        "href": "<url address>"
      }
    ],
    "yardName": "Yard A",
    "auctionDate": "1/25/17"
  },
  {
    "yardNumber": 10,
    "links": [
      {
        "rel": "yardSaleList",
        "href": "<url>"
      }
    ],
    "yardName": "Yard B",
    "auctionDate": "1/25/17"
  }
] 


我的代码是这样的:

TLinkItem = class
public
  [alias('rel')]
  rel: String;
  [alias('href')]
  href: string;
end;    

TPartItem = class
public
  [alias('yardNumber')]
  YardNumber: integer;
  [alias('links')]
  Links: TObjectList<TLinkItem>;
  [alias('yardName')]
  YardName: string;
  [alias('auctionDate')]
  AuctionDate: String; 
  destructor destroy; override;
end;

TPartItems = class  /// not used because this is an unnamed JSON array
public
  [alias('ItemData')] 
  ItemData : TObjectList<TPartItem>;     
end;

...

destructor TPartItems.destroy;
begin
  freeandnil(Links);
  inherited;
end;


如果它是一个命名数组,我可以使用上面的对象来引用名称
的数组:

myData := TPartItems.FromJSON(jsonString); 
showmssage(myData.ItemData.count.toString);


但是因为这是一个未命名的数组,所以我不能这样做。

我希望我只是缺少一些我找不到的细节。
到现在为止,这与其他数据供应商的关系一直很好,但是
我从未遇到过像这样的未命名JSON数组。

最佳答案

我查看了XSuperObject的源代码,没有看到任何使您显示的JSON直接将未命名数组流式传输到TPartItems的东西。因此,我建议将JSON数组简单地包装在JSON对象中,该对象为数组指定名称,例如:

myData := TPartItems.FromJSON('{ItemData: ' + jsonString + '}');

关于json - 获取xsuperobject将未命名的数组解析为tobject结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41557618/

相关文章:

java - Delphi有没有相当于java的OnReceive函数?

delphi - 在最大化丢失分配值之前动态分配表单大小

listview - 如何检测 TListView 组标题上的鼠标单击?

java - 序列化 Jackson JSON 树模型时排除 NullNode

jquery - Facebook 客户端永久访问 token

json - R:将特定行转换为列

jQuery 从外部 JSON 文件或站点获取 JSON

sockets - IP 组播 + UDP : Should I receive data for all groups?

delphi - delphi中如何获取操作系统的语言是双字节?

android - 在Delphi XE7 Android中的Messagedlg