Ver Mensaje Individual
  #1  
Antiguo 24-09-2016
Ramsay Ramsay is offline
Miembro
NULL
 
Registrado: ene 2016
Posts: 104
Reputación: 9
Ramsay Va por buen camino
Leer JSON sin nombre

Estoy estudiando este codigo :

Código Delphi [-]

var
  JSON: TlkJSONobject;
  UnObjetoJSON: TlkJSONobject;
  UnArrayJSON,
  OtroArrayJSON: TlkJSONlist;
  n, i: Integer;

begin
  JSON:=TlkJSONobject.Create;
  try
    // parseamos el JSON
    JSON:=TlkJSON.ParseText(ContenidoJSON) as TlkJSONobject;
    
    // Obtenemos los objetos JSON principales
    UnObjetoJSON:=JSON.Field['nombreObjetoJSON'] as TlkJSONobject;
    UnArrayJSON:=JSON.Field['nombreArrayJSON'] as TlkJSONlist;
    OtroArrayJSON:=JSON.Field['nombreOtroArrayJSON'] as TlkJSONlist;
    // ahora ya podemos obtener el contenido de estos 3 objetos JSON

    // ejmplo chorra de obtener un valor de un objeto "normal" JSON
    if UnObjetoJSON.Field['nombreField'].Value = 'Pepito' then 
      begin
        // ...
      end;

    // para recorrer un JSON que es un array, usamos algo asi
    for n:=0 to UnArrayJSON.Count - 1 do
      if UnArrayJSON.Child[n].Field['nombreField'].Value = 'Pepito' then
        begin
          // ...
        end;

    // si se diera el caso que un objeto JSON (sea normal o array) tiene otro(s) array(s) dentro, es tan facil como...
    for n:=0 to OtroArrayJSON.Count - 1 do
      for i:=0 to OtroArrayJSON.Child[n].Count - 1 do
        // y asi, at infinitum...
  finally
    JSON.Free;
  end;

La pregunta es como puedo leer un json que tiene arrays sin nombre , en este caso lo tiene hecho :

Código Delphi [-]
    for n:=0 to OtroArrayJSON.Count - 1 do
      for i:=0 to OtroArrayJSON.Child[n].Count - 1 do
        // y asi, at infinitum...

Pero si necesita nombres :

Código Delphi [-]
OtroArrayJSON:=JSON.Field['nombreOtroArrayJSON'] as TlkJSONlist;

Explicandome mejor , es un json que tiene varios arrays sin nombre y dentro de cada uno estan los datos ( que si tienen nombre) , yo necesito listar todos los arrays sin nombre para poder entrar a los valores importantes

Mi codigo :

Código Delphi [-]
  JS := TlkJSONobject.Create;
  JS := TlkJSON.ParseText(code) as TlkJSONobject;

  for i := 0 to JS.Count - 1 do
  begin
    if JS.FieldByIndex[i].SelfType <> jsNull then
      memo1.Add(JS.child(i).Field['datapc'].Value);
  end;

¿ Alguien me podria ayudar ?

Última edición por Ramsay fecha: 24-09-2016 a las 22:44:26.
Responder Con Cita