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

前端之家收集整理的这篇文章主要介绍了如何在Delphi XE2中解析嵌套的JSON对象?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 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
原文链接:https://www.f2er.com/delphi/102690.html

猜你在找的Delphi相关文章