How to create a JSON Object in Delphi

Asked

Viewed 695 times

0

I’m trying to create a JSON with values that have been populated by the user. So far I have it, but it’s not working

procedure TOAuth2TesterFrm.Button2Click(Sender: TObject);
  var
    nome_evento: String;
    inicio: String;
    fim: String;
    objeto: TJSONObject;
begin
    objeto.AddPair('nome',nome_evento);
    objeto.AddPair('data_inicio',inicio);
    objeto.AddPair('data_fim',fim);
    LogMem.Text:=TJSON.ObjectToJsonString(objeto);

3 answers

2

From what little I know, it’s missing instantiating the object before using it. Try

var
  objeto: TJSONObject;
begin
  objeto := TJSONObject.Create;
  try
    objeto.AddPair('nome' , nome_evento);
    objeto.AddPair('data_inicio' , inicio);

  finally
    objeto.Free;
  end;
end;

1

It’s missing instantiating your object:

objeto := TJSONObject.Create;

Also remember to destroy the same not to give memory Leak.

0

Instead of using

LogMem.Text:=TJSON.ObjectToJsonString(objeto);

Try

LogMem.Text := objeto.ToString;

or

LogMem.Text := objeto.ToJSON;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.