来自restresponse Json的GET值

时间:2016-06-21 17:48:32

标签: json delphi facebook-graph-api delphi-10-seattle

从这个JSON中检索url密钥的任何方法吗?

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

Facebook's Graph API提供。我在Delphi 10.0 Seattle中使用Rest Library来检索它。

1 个答案:

答案 0 :(得分:6)

首先阅读Embarcadero的JSON documentation。您可以使用System.JSON单元中的类,例如:

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;

或者,如果您使用的是Embarcadero的REST client library,它可以为您检索并预解析JSON:

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;