V
V
Vadim Aliev2021-08-18 04:44:29
Delphi
Vadim Aliev, 2021-08-18 04:44:29

How to create JSON in Delphi XE?

Good day to all! Please tell me how to create a record like this using DBXJSON : I wrote a simple code, but when it is executed, "Invalid Pointer operation" falls out

[{"foo":"bar"},{"foo":"bar"}]

var
  JS : TJSONObject;
  JSA : TJSONArray;
  JSP : TJSONPair;

begin
  JS := TJSONObject.Create;
  JSA := TJSONArray.Create;

  JSP := TJSONPair.Create('Array', JSA);
  JS.AddPair(JSP);

  writeln(JS.ToString);

  JSP.Free;
  JS.Free;
  JSA.Free;

  readln;

end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hemul GM, 2021-08-18
@Redfern89

This is an array of objects. So, we create an array and add objects to it. Is it logical? Yes, I think so.

var
  JSA : TJSONArray;
  JS : TJSONObject;
begin
  JSA := TJSONArray.Create;

  JS := TJSONObject.Create;
  JS.AddPair('foo', 'bar');
  JSA.Add(JS);

  JS := TJSONObject.Create;
  JS.AddPair('foo', 'bar');
  JSA.Add(JS);

  writeln(JSA.ToString);

  JSA.Free;

  readln;
end.

When adding elements to the JSA, you do not need to remove them yourself. They become part of it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question