我正在使用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/