问题描述
我很喜欢JSON,我手上有这个项目,需要我解析JSON并在ListView中显示一些内容。问题是我现在阅读的文档涉及包含JSON数组的JSON对象,而我的案例涉及处理嵌套对象。为了简化故事,下面是总结:我正在使用Delphi XE2与DBXJSON。我向服务器发布了一些值,并且回复了一个如下所示的JSON对象: {
:{
男士运动鞋:{
instock:false,
size:423,
制造商:Adidas,
lastcheck:20120529
},
钱包:{
instock:true,
size:不适用,
制造商:Prada,
lastcheck:20120528
},
Men's Hood:{
instock:false,
大小:M,
制造商:通用,
lastcheck:20120529
}
},
总计:41 ,
可用:30
}
我想要实现的是将每个项目(即钱包)解析并添加为列表视图中的标题,以及一个子项目(制造商)。我创建了一个将JSON字符串作为参数的过程,创建了JSON对象,但我不知道如何进一步解析嵌套对象。
procedure TForm1.ParseString(const AString:string);
var
json:TJSONObject;
jPair:TJSONPair;
jValue:TJSONValue;
jcValue:TJSONValue;
l,i:整数;
begin
json:= TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(AString),0)作为TJSONObject;
尝试
//在这种情况下,获取对评估索引为1
jPair:= json.Get(1);
{进一步处理嵌套对象并将其添加到listview}
finally
json.Free;
结束
结束
任何建议都将非常感激。失去了相当多的时间,试图让Delphi中的JSON的内容无效。
感谢
sphynx
尝试此示例
{$ APPTYPE CONSOLE}
{$ R * .res}
使用
DBXJSON,
System.SysUtils;
Const
StrJson =
'{'+
'products:{'+
'男士运动鞋:{'+
'instock:false,'+
'size:423,'+
'制造商:Adidas,'+
'lastcheck:20120529'+
'},'+
'钱包:{'+
'instock:true,'+
'size:不适用,+
制造商:Prada,'+
'lastcheck:20120528'+
'
'Men's Hood:{'+
'instock:false,'+
'size:M,'+
'制造商:通用,+
lastcheck:20120529'+
'}'+
'},'+
' ,'+
'可用:30'+
'}';
procedure ParseJson;
var
LJsonObj:TJSONObject;
LJPair:TJSONPair;
LP产品:TJSONValue;
LP产品:TJSONValue;
LItem:TJSONValue;
LIndex:整数;
LSize:整数;
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]));如果TJSONPair(LItem).JsonValue是TJSONFalse,然后
Writeln(Format('%s:%s',[]),则TJSONArray(LJPair.JsonValue)中的LItem的
begin
TJSONPair(LItem).JsonString.Value,'false']))
else
如果TJSONPair(LItem).JsonValue是TJSONTrue,然后
Writeln(Format('%s:%s' [TJSONPair(LItem).JsonString.Value,'true']))
else
Writeln(格式('%s:%s',[TJSONPair(LItem).JsonString.Value,TJSONPair ).JsonValue.Value]));
结束
结束
finally
LJsonObj.Free;
结束
结束
begin
try
ParseJson;
除了
在E:Exception do
Writeln(E.ClassName,':',E.Message);
结束
Readln;
结束。
这将返回
产品名称男士运动鞋
instock:false
尺寸:423
制造商:Adidas
lastcheck:20120529
产品名称钱包
:true
尺寸:不适用
制造商:Prada
lastcheck:20120528
产品名称男式外罩
instock:false
尺寸:M
制造商:Generic
lastcheck:20120529
I'm new to JSON and I have this project on my hands that require me to parse a JSON and display some of its contents in a ListView. The problem is that the documentation I've read by now dealt with JSON objects containing JSON arrays, while my case involves dealing with nested objects. To cut the story short, here's the summary: I'm using Delphi XE2 with DBXJSON. I post some values to a server and it replies with a JSON object that looks like that:
{
"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
}
What I wanted to achieve was to have each item (i.e. Purse) parsed and added as caption in a listview, along with one subitem (manufacturer). I created a procedure that takes the JSON string as argument, created the JSON object, but I don't know how to parse the nested objects any further.
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;
Any suggestions would be highly appreciated. Lost quite some time trying to get the ins and outs of JSON in Delphi with no avail.
Thanks,sphynx
Try this sample
{$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.
This will return
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
这篇关于如何在Delphi XE2中解析嵌套的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!