Página 1 de 1

JSON - Criar

Enviado: 15/Mai/2018, 11:07
por fernando
https://stackoverflow.com/questions/211 ... delphi-xe5

Código: Selecionar todos

{
 "APIKEY": "sadfsafsafdsa",
 "UserID": "123123123",
 "Transactions:"
        [{
           "TransactionID": 1,
           "Amount": 23
         },
         {
         "TransactionID": 2,
         "Amount": 53
         }]       
}

Código: Selecionar todos

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Data.DBXJSON,
  System.SysUtils;


var
  LJson, LJsonObject: TJSONObject;
  LArr: TJSONArray;
begin
  try
      ReportMemoryLeaksOnShutdown:=True;
      LJsonObject := TJSONObject.Create;
      try
        LJsonObject.AddPair(TJSONPair.Create('APIKEY', 'sadfsafsafdsa'));
        LJsonObject.AddPair(TJSONPair.Create('UserID', '123123123'));

          LArr := TJSONArray.Create;
          LJson   := TJSONObject.Create;
          LJson.AddPair(TJSONPair.Create('TransactionID', '1'));
          LJson.AddPair(TJSONPair.Create('Amount', '23'));
          LArr.Add(LJson);

          LJson   := TJSONObject.Create;
          LJson.AddPair(TJSONPair.Create('TransactionID', '2'));
          LJson.AddPair(TJSONPair.Create('Amount', '53'));
          LArr.Add(LJson);

          LJsonObject.AddPair(TJSONPair.Create('Transactions', LArr));

        Write(LJsonObject.ToString);

      finally
        LJsonObject.Free;  //free all the child objects.
      end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.