Ver Mensaje Individual
  #4  
Antiguo 03-04-2017
WHILENOTEOF WHILENOTEOF is offline
Miembro
 
Registrado: mar 2008
Posts: 231
Reputación: 17
WHILENOTEOF Va camino a la fama
Cita:
Empezado por AgustinOrtu Ver Mensaje
Podes probar de esta manera:

Código Delphi [-]
uses
  System.IOUtils,
  System.JSON,
  System.SysUtils;

procedure DoIt;
var
  json, geometry, location: TJSONObject;
  results: TJSONArray;
  lat, lng: TJSONNumber;
  I: Integer;
begin
  json := TJSONObject.ParseJSONValue(TFile.ReadAllText(JsonFile)) as TJSONObject;

  try
    results := json.Values['results'] as TJSONArray;   
    geometry := results.Items[0].GetValue('geometry');
    location := geometry.Values['location'] as TJSONObject;
    lat := location.Values['lat'] as TJSONNumber;
    lng := location.Values['lng'] as TJSONNumber;
    Writeln(lat.ToString);
    Writeln(lng.ToString);
  finally
    json.Free;
  end;
end;

Tene en cuenta que no hago ninguna validacion sobre la estructura del json, y asumo que el primer elemento del array "results" es el "geometry"
Con Delphi Seattle el texto marcado me da error. Cambiando por el texto siguiente me funciona bien:

Código Delphi [-]
   s:= TFile.ReadAllText(OpenDialogJson.FileName);
   jValue:= TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(s), 0);
    try
      LJsonObj:= jValue as TJSONObject;

      jPair:= LJsonObj.Get('results');
      jArray:= jPair.JsonValue as TJSONArray;
      for i:= 0 to jArray.Size - 1 do
       begin
        jArray.Items[i].TryGetValue('geometry',geometry);
        location := geometry.Values['location'] as TJSONObject;
        lat:= location.Values['lat'] as TJSONNumber;
        lng:= location.Values['lng'] as TJSONNumber;
       end;

    finally
      jValue.Free;
    end;
Responder Con Cita