Ver Mensaje Individual
  #4  
Antiguo 27-05-2015
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
lbidi,

Cita:
Empezado por lbidi
...Tengo el siguiente array en formato json y necesito parsearlo...


Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, uLkJSON;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Read and Parse File JSON
procedure TForm1.Button1Click(Sender: TObject);
var
   SL, FieldList : TStringList;
   JS : TlkJSONobject;
   i : Integer;

begin

   SL := TStringList.Create;
   SL.LoadFromFile(ExtractFilePath(Application.ExeName) + 'FileTest.json');

   FieldList := TStringList.Create;

   JS := TlkJSONobject.Create;
   JS := TlkJSON.ParseText(SL.Text) as TlkJSONobject;

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

   ListBox1.Clear;
   for i := 0 to FieldList.Count - 1 do
      ListBox1.Items.Add(FieldList.Strings[i]);

   SL.Free;
   FieldList.Free;
   JS.Free;

end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, Lee y analiza la estructura de un archivo .json (Msg #1) por medio de la librería lkJSON-1.07, como se muestra en la siguiente imagen:



Nota: La librería lkJSON-1.07, no necesita ser instalada, solo se debe copiar la unidad uLkJSON.pas al directorio del proyecto o agregar la ruta de la misma en el Library Path de Delphi.

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 27-05-2015 a las 05:38:21.
Responder Con Cita