我是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的详细信息,但无济于事。
谢谢,sphynx
试试这个样本
{$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',[TJSONPair(LItem).JsonString.Value, 'true'])) else Writeln(Format(' %s : %s',[TJSONPair(LItem).JsonString.Value, 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