JSON - Ler

fernando
Administrador
Mensagens: 130
Registrado em: 18/Ago/2015, 19:03

JSON - Ler

Mensagem por fernando » 15/Mai/2018, 11:17

Código: Selecionar todos

uses System.JSON;

Código: Selecionar todos

{"Nome":"Eu Miesmo","Email":"[email protected]"}

Código: Selecionar todos

var
  joName : TJSONObject;
Const
  StrJson : string = '{"Nome":"Eu Miesmo","Email":"[email protected]"}';
begin
  joName := TJSONObject.ParseJSONValue(StrJson) as TJSONObject;
  CodeSite.Send('Nome : ' + joName.GetValue('Nome').Value);
  CodeSite.Send('Email : ' + joName.GetValue('Email').Value);

Código: Selecionar todos

[{"Nome":"Fernando Galdino","Email":"[email protected]"},{"Nome":"ManiacoZ","Email":"[email protected]"}]

Código: Selecionar todos

var
  LJsonArr   : TJSONArray;
  LJsonValue : TJSONValue;
  LItem     : TJSONValue;
Const
  StrJson : string = '[{"Nome":"Fernando Galdino","Email":"[email protected]"},{"Nome":"ManiacoZ","Email":"[email protected]"}]';
begin
   LJsonArr    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONArray;
   for LJsonValue in LJsonArr do
   begin
    CodeSite.Send('Nome : ' + LJsonValue.GetValue<string>('Nome'));
    CodeSite.Send('Email : ' + LJsonValue.GetValue<string>('Email'));
   end;

Código: Selecionar todos

{"result":["[{"email":"[email protected]","regid":"12312312312312312313213w"},{"email":"[email protected]","regid":"AAAAAAA"}]"]}

Código: Selecionar todos

{$APPTYPE CONSOLE}

uses
  SysUtils, JSON;

const
  response =
    '{"result":["[{\"email\":\"[email protected]\",\"regid\":\"12312312312312312313213w\"},'+
    '{\"email\":\"[email protected]\",\"regid\":\"AAAAAAA\"}]"]}';

procedure Main;
var
  LResult: TJSONArray;
  LJsonResponse: TJSONObject;
  ja: TJSONArray;
  jv: TJSONValue;
begin
  LJsonResponse := TJSONObject.ParseJSONValue(response) as TJSONObject;
  LResult := LJsonResponse.GetValue('result') as TJSONArray;
  ja := TJSONObject.ParseJSONValue(LResult.Items[0].Value) as TJSONArray;
  for jv in ja do begin
    Writeln(jv.GetValue<string>('email'));
    Writeln(jv.GetValue<string>('regid'));
  end;
end;

begin
  try
    Main;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.