Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Internet
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Colaboración Paypal con ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #4  
Antiguo 03-04-2017
WHILENOTEOF WHILENOTEOF is offline
Miembro
 
Registrado: mar 2008
Posts: 234
Poder: 19
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
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Obtener los documentos creados en Google Drive pamda Delphi para la web 4 13-11-2014 18:16:00
Json de PHP a Imagen Kubelo Gráficos 5 20-06-2014 09:05:59
Fecha JSON. BuenaOnda Varios 4 27-11-2011 18:46:27
Obtener el Menu de Google Earth para usarlo en mi aplicacion vejerf API de Windows 2 26-05-2008 08:49:26
Últimas tres fechas de un registro con la misma latitud y longitud mcalmanovici MySQL 8 20-09-2007 17:00:58


La franja horaria es GMT +2. Ahora son las 22:36:22.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi