Deserialize a json lazarus / 21-General ---------------------------------------------------------------------- Hansvb · 2022-11-06 11:10 Hi, I want to deserialize a json to a treeview without knowing what's in the json beforehand. I have the following so far. I get the first two objects out, but there is much more. And the objects are stepped. How can i get to them? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  jData, jItem : TJSONData;   i: integer;  listitemsjson:string;  objectname : String;begin  listitemsjson := ReadURLGet('https://api.....');  jData  := GetJSON(listitemsjson);   MemoFormatted.Text := FormatJsonData(jData); // Format the json (pretty print).   for i := 0 to jData.Count - 1 do    begin      jItem := jData.Items[i];      objectname := TJSONObject(jData).Names[i];       ListBox1.Items.Add(objectname);    Treeview1.Items.Add(nil, objectname);    end;end; ---------------------------------------------------------------------- wp · 2022-11-06 11:23 There's a detailed article by Michael Van Canneyt about this topic: https://www.freepascal.org/~michael/articles/webdata/webdata.pdf ---------------------------------------------------------------------- bytebites · 2022-11-06 12:24 There is jsonviewer application in lazarus/tools folder. ---------------------------------------------------------------------- Hansvb · 2022-11-07 11:16 I wll look at the pdf. It looks good. ---------------------------------------------------------------------- Hansvb · 2022-11-08 14:17 I'm a little further along, but I'm still a long way from seeing it. For now I just want to put the objects in a memo. The below shows only the first objects and not the nested objects. How can I retrieve the nested objects? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm_main.getObjectNames(jsonDT: TJSONData; List : TStringList): TStringList;var  jData : TJSONData;  jObject : TJSONObject;  i,j : Integer;begin  Result:= TStringList.Create;  Result := List;  try    If (jsonDT.Count<>0) then      begin        jData := jsonDT;        if (jData is TJSONObject) then          begin            jObject := jData as TJSONObject;            for I:=0 to jObject.Count-1 do            begin                Result.Add(jObject.Names[i]);                 if (jObject.Items[i] is TJSONObject) then                  begin                  jdata := jObject.Items[i];                    getObjectNames(jdata, Result);                  end;              end;          end;      end;  except    FreeAndNil(Result);    raise;  end;end; ---------------------------------------------------------------------- Leledumbo · 2022-11-08 21:36 Quote from: Hansvb on November 08, 2022, 03:17:54 pmI'm a little further along, but I'm still a long way from seeing it. For now I just want to put the objects in a memo. The below shows only the first objects and not the nested objects. How can I retrieve the nested objects? The same way you do with the first objects, you just have to recurse to the same method, probably with modifications regarding how you would add the nested objects as it go deeper and deeper. Here's a little demo: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+} uses  Classes,  SysUtils,  fpjson,  jsonparser; const  TheJSONAsString = '{'                  + '  "key1": "string1",'                + '  "key2": {'                  + '    "key21": {'                + '      "key211": 255,'                  + '      "key212": 25.5'                  + '    }'                  + '  },'                + '  "key3": {'                  + '    "key31": true,'                + '    "key32": [1,2,3],'                  + '    "key33": null'                  + '  }'                  + '}'; procedure InsertIntoStringList(const AData: TJSONData; const AList: TStrings);   procedure InsertIntoStringListLevel(const AData: TJSONData; const AList: TStrings; const ALevel: Integer);  var    LPad,LKey: String;  LArr: TJSONArray;    LObj: TJSONObject;    i: Integer;  begin    LPad := StringOfChar('-', ALevel);    if Length(LPad) > 0 then LPad := LPad + ' ';    case AData.JSONType of      jtUnknown: begin        WriteLn(StdErr,'Unknown JSON node type encountered');        Halt(1);      end;      jtNumber: AList.Add(LPad + FloatToStr(AData.AsFloat));    jtString: AList.Add(LPad + '"' + AData.AsString + '"');      jtBoolean: AList.Add(LPad + BoolToStr(AData.AsBoolean, true));      jtNull: AList.Add(LPad + 'NULL');      jtArray: begin        LArr := AData as TJSONArray;        for i := 0 to LArr.Count - 1 do          InsertIntoStringListLevel(LArr.Items[i], AList, ALevel + 1);      end;      jtObject: begin        LObj := AData as TJSONObject;        for i := 0 to LObj.Count - 1 do begin          LKey := LObj.Names[i];          AList.Add(LPad + LKey);          InsertIntoStringListLevel(LObj.Elements[LKey], AList, ALevel + 1);      end;      end;    end;  end; begin  InsertIntoStringListLevel(AData, AList, 0);end; var  LJSON: TJSONData;  LStrList: TStrings;  LStr: String;begin  LJSON := GetJSON(TheJSONAsString);  LStrList := TStringList.Create;  try    InsertIntoStringList(LJSON, LStrList);    for LStr in LStrList do WriteLn(LStr);  finally    LJSON.Free;    LStrList.Free;  end;end. ---------------------------------------------------------------------- Hansvb · 2022-11-14 14:35 Hi, I found the answer in the jsonviewer example. QuoteThere is jsonviewer application in lazarus/tools folder. It has a procedure: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TJSONTab.ShowJSONData(AParent : TTreeNode; Data : TJSONData); This loops through the json.