How to parse specified value from JSON object in Delphi XE3?
Asked Answered
P

3

6

My JSON object looks like this:

{
   "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"
}

I will need the "504 km" value from distance. How can i do this?

Prandial answered 16/10, 2013 at 23:18 Comment(2)
Did you attempt to work this out yourself using the docs and web search?Misdirection
possible duplicate of How to parse a JSON string in Delphi?Eulogize
P
9

You can use the DBXJSON unit, included since Delphi 2010.

Try this sample

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;
Phiphenomenon answered 17/10, 2013 at 0:38 Comment(2)
To be clear, doesn't this only come with the Enterprise edition or the C/S pack? And my goodness the Delphi JSON implementation is ugly. :-( SuperObject is a lot cleaner. It's like EMBT doesn't remember what RAD's about anymore. :-(Orcinol
@Prandial I wrote the sample using XE5, so it seems which the GetValue method was introduced in this Delphi version. I just modified the sample code using the Get method instead.Phiphenomenon
Q
7

One of the libraries that can parse JSON is superobject.

To get rows.elements.distance from your JSON, code would look like this:

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;
Queridas answered 16/10, 2013 at 23:49 Comment(0)
H
0

with json4delphi as navigate:

ajt:= TJson.create();
ajt.Parse(StrJson); 
println('dist: '+ajt['rows'].asarray[0].asObject['elements'].asarray[0].asobject['distance'].asobject['text'].asstring);
ait.Free;

To get the elements name:

 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; 
Hostile answered 9/3, 2021 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.