Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
  }
}
like image 884
Area50plus1 Avatar asked Oct 06 '15 11:10

Area50plus1


People also ask

How do you write JSON?

A JSON Object is an entity in JSON which is enclosed in curly brackets. It is written in the unordered set of name and value pairs in which the name should be followed by “:” (colon), and the name/value pairs need to be separated using “,” (comma). It can be used when key names are arbitrary strings.

What is JSON text format?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

What is JSON example?

JSON Object ExampleA JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by colon. Each entry (key/value pair) is separated by comma.

Can you create a JSON file in Notepad?

In Notepad++ on the Language menu you will find the menu item - 'J' and under this menu item chose the language - JSON. Once you select the JSON language then you won't have to worry about how to save it.


1 Answers

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.

like image 95
fantaghirocco Avatar answered Oct 05 '22 06:10

fantaghirocco