Alexandre Prazeres Alexandre Prazeres - 5 months ago 81
JSON Question

GET Value from restresponse Json

Any way to retrieve

url
key from this JSON?

{
"data": {
"is_silhouette": false,
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/13178742_10205047662871072_6233795154346712405_n.jpg?oh=194b0150c2325660390490779bf9b942&oe=57C22031&__gda__=1472746057_dc9b0557adc8408840fafb73ed325ef8"
}
}


It is provided by Facebook's Graph API. I'm using a Rest Library in Delphi 10.0 Seattle to retrieve it.

Answer

Start by reading Embarcadero's JSON documentation. You can use the classes in the System.JSON unit, eg:

uses
  ..., System.JSON;

var
  json: string;
  obj, data: TJSONObject;
  url: string;
begin
  json := ...; // JSON data retrieved from REST server
  obj := TJSONObject.ParseJSONValue(json) as TJSONObject;
  try
    data := obj.Values['data'] as TJSONObject;
    url := data.Values['url'].Value;
  finally
    obj.Free;
  end;
end;

Alternatively, if you are using Embarcadero's REST client library, it can retrieve and pre-parse the JSON for you:

var
  obj, data: TJSONObject;
  url: string;
begin
  RESTRequest1.Execute;
  obj := RESTResponse1.JSONValue as TJSONObject;
  data := obj.Values['data'] as TJSONObject;
  url := data.Values['url'].Value;
end;
Comments