加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Delphi XE2中解析嵌套的JSON对象?

发布时间:2020-12-15 10:09:31 所属栏目:大数据 来源:网络整理
导读:我是 JSON的新手,我手上有这个项目,需要我解析JSON并在ListView中显示一些内容.问题是我现在阅读的文档涉及包含JSON数组的JSON对象,而我的案例涉及处理嵌套对象.为了简化故事,下面是总结:我正在使用Delphi XE2与DBXJSON.我向服务器发布了一些值,并且回复了
我是 JSON的新手,我手上有这个项目,需要我解析JSON并在ListView中显示一些内容.问题是我现在阅读的文档涉及包含JSON数组的JSON对象,而我的案例涉及处理嵌套对象.为了简化故事,下面是总结:我正在使用Delphi XE2与DBXJSON.我向服务器发布了一些值,并且回复了一个如下所示的JSON对象:
{
    "products": {
        "Men's Sneakers": {
            "instock": false,"size": "423","manufacturer": "Adidas","lastcheck": "20120529"
        },"Purse": {
            "instock": true,"size": "not applicable","manufacturer": "Prada","lastcheck": "20120528"
        },"Men's Hood": {
            "instock": false,"size": "M","manufacturer": "Generic","lastcheck": "20120529"
       }
    },"total": 41,"available": 30
}

我想要实现的是将每个项目(即钱包)解析并添加为列表视图中的标题,以及一个子项(制造商).我创建了一个将JSON字符串作为参数的过程,创建了JSON对象,但我不知道如何进一步解析嵌套对象.

procedure TForm1.ParseString(const AString: string);
var
  json          : TJSONObject;
  jPair         : TJSONPair;
  jValue        : TJSONValue;
  jcValue       : TJSONValue;
  l,i           : Integer;
begin
    json    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(AString),0) as TJSONObject;
  try
    //get the pair to evaluate in this case the index is 1
    jPair   := json.Get(1); 
        {further process the nested objects and adding them to the listview}
  finally
     json.Free;
  end;
end;

任何建议将被高度赞赏.失去了相当一段时间,试图让Delphi中的JSON和内存无效.

谢谢,
斯芬克斯

解决方法

尝试这个样品
{$APPTYPE CONSOLE}

{$R *.res}

uses
  DBXJSON,System.SysUtils;


Const
StrJson=
'{'+
'    "products": {'+
'        "Men''s Sneakers": {'+
'            "instock": false,'+
'            "size": "423",'+
'            "manufacturer": "Adidas",'+
'            "lastcheck": "20120529"'+
'        },'+
'        "Purse": {'+
'            "instock": true,'+
'            "size": "not applicable",'+
'            "manufacturer": "Prada",'+
'            "lastcheck": "20120528"'+
'        },'+
'        "Men''s Hood": {'+
'            "instock": false,'+
'            "size": "M",'+
'            "manufacturer": "Generic",'+
'            "lastcheck": "20120529"'+
'        }'+
'    },'+
'    "total": 41,'+
'    "available": 30'+
'}';

procedure ParseJson;
var
  LJsonObj  : TJSONObject;
  LJPair    : TJSONPair;
  LProducts : TJSONValue;
  LProduct  : TJSONValue;
  LItem     : TJSONValue;
  LIndex    : Integer;
  LSize     : Integer;
begin
    LJsonObj    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
  try
     LProducts:=LJsonObj.Get('products').JsonValue;
     LSize:=TJSONArray(LProducts).Size;
     for LIndex:=0 to LSize-1 do
     begin
      LProduct := TJSONArray(LProducts).Get(LIndex);
      LJPair   := TJSONPair(LProduct);
      Writeln(Format('Product 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','true']))
           else
            Writeln(Format('  %s : %s',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.

这将返回

Product Name Men's Sneakers
  instock : false
  size : 423
  manufacturer : Adidas
  lastcheck : 20120529
Product Name Purse
  instock : true
  size : not applicable
  manufacturer : Prada
  lastcheck : 20120528
Product Name Men's Hood
  instock : false
  size : M
  manufacturer : Generic
  lastcheck : 20120529

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读