获取xsuperobject将未命名的数组解析为tobject结构 [英] Get xsuperobject to parse unnamed array to tobject structure

查看:244
本文介绍了获取xsuperobject将未命名的数组解析为tobject结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Delphi 10.1 Berlin Update 2, and am trying to use XSuperObject / XSuperJSON to take a JSON response from a 3rd party provider and parse it into an object structure.

这是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;

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

If it were a named array, I could use the above object to refer to the name of the array:

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

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

But because this is an unnamed array, I can't do that.

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

I'm hoping I have just missing some kind of detail here that I could not find. Until now, this has worked pretty well with other data suppliers, but I have never run across an unnamed JSON array like this.

推荐答案

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

I looked at XSuperObject's source and do not see anything that would allow the JSON you have shown to stream an unnamed array into TPartItems directly. So, I would suggest simply wrapping the JSON array inside of a JSON object that gives the array a name, eg:

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

这篇关于获取xsuperobject将未命名的数组解析为tobject结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆