Search code examples
jsondelphidelphi-7

Write a json text


I'm using lkJSON-1.07 in delphi 7, in their example

js := TlkJSONobject.Create;
js.Add('namestring','namevalue');
// get the text of object
s := TlkJSON.GenerateText(js);
  writeln(s);
writeln;
writeln('more readable variant:');
// (ver 1.03+) generate readable text
i := 0;
s := GenerateReadableText(js,i);
writeln(s);

js.Free;

it generate a text like this: { "namestring":"namevalue" }

How do I write a json text format like this one:

{
  "Users": 
  {
    "test_user1":
    {
        "time":1600,
        "Points":4
    }
    "test_user2":
    {
        "time":1500,
        "Points":3
    }
  }
}

Solution

  • While using the JSON Delphi Library you have to adopt the method which follows in order to add child JSON elements to their parents:

    function TlkJSONobject.Add(const aname: WideString; aobj: TlkJSONbase): Integer;
    

    The method allows the aobj parameter to be attached as a child of an aname element.

    The code below allows to accomplish your task:

    var
      js0, js1, js2, js22: TlkJSONobject;
      s: string;
      i: Integer;
    begin
      js2 := TlkJSONobject.Create;
      js2.Add('time', '1600');
      js2.Add('Points', 4);
    
      js22 := TlkJSONobject.Create;
      js22.Add('time', '1500');
      js22.Add('Points', 3);
    
      js1 := TlkJSONobject.Create;
      js1.Add('test_user1', js2);          
      js1.Add('test_user2', js22);
    
      js0 := TlkJSONobject.Create;
      js0.Add('Users', js1);
    
      i := 0;
      s := GenerateReadableText(js0, i);
      WriteLn(s);
    
      js0.Free;
    end;
    

    This is a more suitable way to write the previous code - but less readable in my opinion.

    The idea here is to create the elements in the natural parent-child relationship order: the children are added to the already inserted parent using the Field property of the TlkJSONobject object.

    Please notice that js.Field['some string'] is the same as js['some string'] because of the default directive applied to the Field property.

    var
      js: TlkJSONobject;
      s: string;
      i: Integer;
    begin
      js := TlkJSONobject.Create;
      try
        js.Add('Users', TlkJSONobject.Create);
    
        with TlkJSONobject(js['Users']) do begin
          Add('test_user1', TlkJSONobject.Create);
          Add('test_user2', TlkJSONobject.Create);
        end;
    
        with TlkJSONobject(TlkJSONobject(js['Users'])['test_user1']) do begin
          Add('time', '1600');
          Add('Points', 4);
        end;
    
        with TlkJSONobject(TlkJSONobject(js['Users'])['test_user2']) do begin
          Add('time', '1500');
          Add('Points', 3);
        end;
    
        i := 0;
        s := GenerateReadableText(js, i);
        WriteLn(s);
    
      finally
        js.Free;
      end;
    end;
    

    Running the project, it prints:

    {
         "Users":{
             "test_user1":{
                 "time":"1600",
                 "Points":4
             },
             "test_user2":{
                 "time":"1500",
                 "Points":3
             }
         }
    }
    

    In a real case, you obviously will consider to create the objects and append the children using some loop instruction.