如何在Delphi XE3中解析JSON对象中的指定值?
我的JSON对象如下所示:如何在Delphi XE3中解析JSON对象中的指定值?,json,delphi,firemonkey,delphi-xe3,Json,Delphi,Firemonkey,Delphi Xe3,我的JSON对象如下所示: { "destination_addresses" : [ "Paris, France" ], "origin_addresses" : [ "Amsterdam, Nederland" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "504 km",
{
"destination_addresses" : [ "Paris, France" ],
"origin_addresses" : [ "Amsterdam, Nederland" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "504 km",
"value" : 504203
},
"duration" : {
"text" : "4 uur 54 min.",
"value" : 17638
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
var
json : ISuperObject;
row_item : ISuperObject;
elements_item: ISuperObject;
begin
json := TSuperObject.ParseFile('C:\json.txt', TRUE); // load whole json here
for row_item in json['rows'] do // iterate through rows array
for elements_item in row_item['elements'] do // iterate through elements array
begin
WriteLn(elements_item['distance'].S['text']); // get distance sub-json and it's text key as string
end;
end;
我需要距离的“504公里”值。我如何才能做到这一点?可以解析JSON的库之一是 要从JSON获取
rows.elements.distance
,代码如下所示:
{
"destination_addresses" : [ "Paris, France" ],
"origin_addresses" : [ "Amsterdam, Nederland" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "504 km",
"value" : 504203
},
"duration" : {
"text" : "4 uur 54 min.",
"value" : 17638
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
var
json : ISuperObject;
row_item : ISuperObject;
elements_item: ISuperObject;
begin
json := TSuperObject.ParseFile('C:\json.txt', TRUE); // load whole json here
for row_item in json['rows'] do // iterate through rows array
for elements_item in row_item['elements'] do // iterate through elements array
begin
WriteLn(elements_item['distance'].S['text']); // get distance sub-json and it's text key as string
end;
end;
您可以使用自Delphi 2010以来提供的装置
试试这个样品
uses
DBXJSON;
{$R *.fmx}
Const
StrJson=
'{ '+
' "destination_addresses" : [ "Paris, France" ], '+
' "origin_addresses" : [ "Amsterdam, Nederland" ], '+
' "rows" : [ '+
' { '+
' "elements" : [ '+
' { '+
' "distance" : { '+
' "text" : "504 km", '+
' "value" : 504203 '+
' }, '+
' "duration" : { '+
' "text" : "4 uur 54 min.", '+
' "value" : 17638 '+
' }, '+
' "status" : "OK" '+
' } '+
' ] '+
' } '+
' ], '+
' "status" : "OK" '+
'}';
procedure TForm6.Button1Click(Sender: TObject);
var
LJsonObj : TJSONObject;
LRows, LElements, LItem : TJSONValue;
begin
LJsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
try
LRows:=LJsonObj.Get('rows').JsonValue;
LElements:=TJSONObject(TJSONArray(LRows).Get(0)).Get('elements').JsonValue;
LItem :=TJSONObject(TJSONArray(LElements).Get(0)).Get('distance').JsonValue;
ShowMessage(TJSONObject(LItem).Get('text').JsonValue.Value);
finally
LJsonObj.Free;
end;
end;
使用json4delphi作为导航:
ajt:= TJson.create();
ajt.Parse(StrJson);
println('dist: '+ajt['rows'].asarray[0].asObject['elements'].asarray[0].asobject['distance'].asobject['text'].asstring);
ait.Free;
要获取元素名称,请执行以下操作:
jOb:= ajt.JsonObject;
for cnt:= 2 to job.count-2 do begin
Clabel:= job.items[cnt].name;
JsArr:= job.values[Clabel].asArray;
for cnt2:= 0 to jsarr.count-1 do
jsobj:= jsarr.items[cnt2].asobject;
for cnt3:= 0 to jsobj.count do
writeln(jsobj['elements'].asarray[0].asobject.items[cnt3].name)
end;
要明确的是,这不只是随企业版或C/S包一起提供的吗?我的天哪,Delphi JSON实现非常糟糕-(SuperObject更干净了。好像EMBT已经不记得RAD是怎么回事了。)-(@Frank我使用XE5编写了示例,因此在这个Delphi版本中似乎引入了
GetValue
方法。我只是使用Get
方法修改了示例代码。您是否尝试使用文档和web搜索来解决这个问题?可能是重复的