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
}
}
}
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With