Create (TJSon)

De Wiki1000
constructor Create(const iJsonString:string);

Cette méthode instancie un objet TJSon et parse le document.

Exemples:

//Procedure JsonParser;
var json:TJson; S,stag:string; idx:Integer;
begin
  json := TJson.Create(
   '{
    "employees": [
    { "firstName":"John" , "lastName":"Doe" },
    { "firstName":"Anna" , "lastName":"Smith" },
    { "firstName":"Peter" , "lastName":"Jones" }
    ]
    }'
  );
 
  S := '';
  for idx:=0 to json.employees.count-1 do
   begin
     S := S+stag+json.employees[idx].firstName;
     stag := ',';
   end;
  showMessage(S);
end;

Exemple, qualification d'une adresse avec Google geocode :

//Procedure TestGeocode;
var http:THttpClient; json:TJson; lat,long:Double;
begin
  http := ThttpClient.Create(nil);
  http.URL := 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=71 Route d''Enghien,92160,France';
  try
    http.Execute;
    json := TJson.Create(http.ResponseStr);
    lat := StrToFloat(json.results[0].geometry.location.lat);
    long := StrToFloat(json.results[0].geometry.location.lng);
    ShowMessage(Format('%0.5f,%0.5f',[lat,long]));
  except
    ShowMessage(http.ErrorText);
  end;
end;

Voir aussi:

Outils personnels