function TForm1.ParseTiempoCiudadesHist(AObjResp: TlkJSONobject; var ATiempoProxHoras: TTiempoProxHoras): Boolean;
var
i, j, num, index:integer;
oHorasList, OWList:TlkJSONlist;
oHora, oCoord, oMain, oWind, oWeather:TlkJSONobject;
Str:String;
begin
// ini
Result := False;
// Si no está asignado salimos..
if not Assigned(AobjResp) then begin
Exit;
end;
// Si hay error no parseamos
if IsErrorResponse(AObjResp, errCode, ErrMsg) then begin
Exit;
end;
// proteccion para el parseo
try
// cod. devuelto (datos principales
ATiempoProxHoras.Cod := errCode;
num := AObjResp.IndexOfName('count');
if (num <> -1) then begin
num := GetAsInteger(AObjResp.Field['count'].Value);
end;
// si no hay ciudades
if (num = 0) then begin
MessageDlg('No hay ninguna ciudad que coincida con ese código [nombre,pais].', mtWarning, [mbOK], 0);
Exit;
end;
// Lista de horas (Lista)
TlkJSONBase(oHorasList) := AObjResp.Field['list'];
// array de elementos
SetLength(ATiempoProxHoras.TiempoHora, oHorasList.Count);
// Quedarse con el primier elemento de la lista...
for i := 0 to (oHorasList.Count - 1) do begin
// datos de la primera ciudad
TlkJSONBase(oHora) := oHorasList.Child[i];
// datos básicos
ATiempoProxHoras.TiempoHora[i].dt_text := GetAsString(oHora.Field['dt_txt'].Value);
// convertir fecha-Hora
Str := ATiempoProxHoras.TiempoHora[i].dt_text;
ATiempoProxHoras.TiempoHora[i].dt := EncodeDateTime(
StrToIntdef(Copy(Str, 1, 4), 0),
StrToIntdef(Copy(Str, 6, 2), 0),
StrToIntdef(Copy(Str, 9, 2), 0),
StrToIntdef(Copy(Str, 12, 2), 0),
StrToIntdef(Copy(Str, 15, 2), 0),
StrToIntdef(Copy(Str, 18, 2), 0), 0);
// Load Main
TlkJSONBase(oMain) := oHora.Field['main'];
ATiempoProxHoras.TiempoHora[i].Main.temp := GetAsFloat(oMain.Field['temp'].Value);
ATiempoProxHoras.TiempoHora[i].Main.tempmin := GetAsFloat(oMain.Field['temp_min'].Value);
ATiempoProxHoras.TiempoHora[i].Main.tempmax := GetAsFloat(oMain.Field['temp_max'].Value);
ATiempoProxHoras.TiempoHora[i].Main.pressure := GetAsFloat(oMain.Field['pressure'].Value);
ATiempoProxHoras.TiempoHora[i].Main.humidity := GetAsInteger(oMain.Field['humidity'].Value);
// Load weather
TlkJSONBase(OWList) := oHora.Field['weather'];
TlkJSONBase(oWeather) := oWList.Child[0];
ATiempoProxHoras.TiempoHora[i].Weather.id := GetAsInteger(oWeather.Field['id'].Value);
ATiempoProxHoras.TiempoHora[i].Weather.main := GetAsString(oWeather.Field['main'].Value);
ATiempoProxHoras.TiempoHora[i].Weather.desc := GetAsString(oWeather.Field['description'].Value);
ATiempoProxHoras.TiempoHora[i].Weather.icon := GetAsString(oWeather.Field['icon'].Value);
end;
Result := True;
except
// si hay error, FALSe
Result := False;
end;
end; |