如何从Delphi XE3中的JSON对象解析指定的值?
发布时间:2020-12-15 04:24:52 所属栏目:大数据 来源:网络整理
导读:我的 JSON对象如下所示: { "destination_addresses" : [ "Paris,France" ],"origin_addresses" : [ "Amsterdam,Nederland" ],"rows" : [ { "elements" : [ { "distance" : { "text" : "504 km","value" : 504203 },"duration" : { "text" : "4 uur 54 min."
我的
JSON对象如下所示:
{ "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" } 我需要距离的“504 km”值.我怎样才能做到这一点? 解决方法
您可以使用自Delphi 2010以来的
DBXJSON 单元.
试试这个样本 uses DBXJSON; {$R *.fmx} Const StrJson= '{ '+ ' "destination_addresses" : [ "Paris,'+ ' "origin_addresses" : [ "Amsterdam,'+ ' "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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |