How to create a complex Tjsonobject/Tjsonarray with XE2

Asked

Viewed 72 times

1

I need to create a complex Json, with several objects, and each object will have items;

{
"Pedidos": [
    {
        "CodCad": "1286",
        "CodForPag": "15",
        "CodCndPag": "43",
        "CodUndAdm": "1",
        "DatEmi": "19/05/2021",
        "DatSol": "26/04/2021 21:42:00",
        "PesCnt": "GLADSON",
        "Tel": "0000000000",
        "Sta": "0",
        "Itens": [
            {
                "CodPro": "310",
                "CodUnd": "66",
                "Qtd": "10",
                "Vlr": "11",
                "Und": "UN",
                "Cmp": "0",
                "Lrg": "0",
                "M2m": "0",
                "Sta": "0",
                "VlrDsc": "0",
                "VlrTot": "110",
                "PrdSrv": "0",
                "NumRqc": "",
                "NumMapCol": "1",
                "EspExt": "60PVD",
                "QtdAtd": "0"
            },
            {
                "CodPro": "3251",
                "CodUnd": "66",
                "Qtd": "3",
                "Vlr": "15",
                "Und": "UN",
                "Cmp": "0",
                "Lrg": "0",
                "M2m": "0",
                "Sta": "0",
                "VlrDsc": "0",
                "VlrTot": "45",
                "PrdSrv": "0",
                "NumRqc": "",
                "NumMapCol": "1",
                "EspExt": "",
                "QtdAtd": "0"
            }
        ]
    },
    ...
    ...
]

}

I realize that when I create a Tjsonobjet and Tjsonarray, and I go Free in them generate me a violation.

JSon := TJsonObject.Create();
APedidos := TJsonArray.Create();
while not cdsMapOrd001.Eof do
begin

I need to use Freeandnil on all created objects ?

1 answer

2


Here the objects are passed as reference, so be careful when destroying. Come on.

The process is simple, here it is like the XML Nodes, we have Father and Son. In this world, if you destroy a Father the son will automatically be destroyed.

Therefore, when trying to destroy a child there will be a violation.

Example of the structure:

var
  i: Integer;
  vJsonResultado: TJSONObject;
  vJsonItensArray: TJSONObject;
  vJsonItens: TJSONArray;
begin
  vJsonResultado := TJSONObject.Create;
  vJsonResultado.AddPair('codigo', '0');

  vJsonItens := TJSONArray.Create;

  for i := 1 to 2 do
  begin
    vJsonItensArray := TJSONObject.Create;
    vJsonItensArray.AddPair(1.ToString, DateToStr(Now));

    vJsonItens.AddElement(vJsonItensArray);
  end;

  vJsonResultado.AddPair('mensagem', vJsonItens);

  FreeAndNil(vJsonResultado);
end;

Note that we are only destroying the vJsonResultado;

Activate the property in your project ReportMemoryLeaksOnShutdown so you can understand where this leaking memory.

ReportMemoryLeaksOnShutdown := True

  • Thank you very much. Clarified. According to what you said I researched and found this topic : https://stackoverflow.com/questions/44584751/reportmemoryleaksonshutdown-not-working-in-delphi-10-2-tokyo and the answer speaks of Xe2.

  • I didn’t understand the use of ReportMemoryLeaksOnShutdown := True It presents a screen with a lot of information, but nothing in detail. You would have some suggestion for studies ?

  • This screen that appears with A lot of information is telling you what was left in your application without being destroyed.

  • 2

    Complementing Junior’s answer, I advise checking with if Assigned(vJsonResulted) then at the end of the object. Just like the other objects created and remove from memory with vJsonResult := nil; vJsonResulted.free. Because handling with JSON objects can give problem with "pointer error" when finishing Freeandnil().

Browser other questions tagged

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