下载地址https://github.com/ahausladen/JsonDataObjects

Simple example

var
Obj: TJsonObject;
begin
Obj := TJsonObject.Parse('{ "foo": "bar", "array": [ 10, 20 ] }') as TJsonObject;
try
ShowMessage(Obj['foo']);
ShowMessage(IntToStr(Obj['array'].Count));
ShowMessage(IntToStr(Obj['array'].Items[]));
ShowMessage(IntToStr(Obj['array'].Items[]));
finally
Obj.Free;
end;
end;
Filling and serializing JSON objects var
Obj, ChildObj: TJsonObject;
begin
Obj := TJsonObject.Create;
try
// easy access
Obj['foo'] := 'bar';
// normal (and faster) access
Obj.S['bar'] := 'foo';
// automatic array creation, Obj is the owner of 'array'
Obj.A['array'].Add();
Obj.A['array'].Add();
// automatic object creation, 'array' is the owner of ChildObj
ChildObj := Obj.A['array'].AddObject;
ChildObj['value'] := 12.3;
// automatic array creation, ChildObj is the owner of 'subarray'
ChildObj.A['subarray'].Add();
ChildObj.A['subarray'].Add(); ShowMessage(Obj.ToJSON({Compact:=}False));
finally
Obj.Free;
end;
{
"foo": "bar",
"bar": "foo",
"array": [
10,
20,
{
"value": 12.3,
"subarray": [
100,
200
]
}
]
}
Copying JSON objects with Assign var
Obj, ClonedObj: TJsonObject;
begin
Obj := TJsonObject.ParseUtf8('{ "foo": [ "bar", {}, null, true, false, { "key": "value" } ] }') as TJsonObject;
try
ClonedObj := TJsonObject.Create;
try
// Make a copy of Obj
ClonedObj.Assign(Obj);
ShowMessage(ClonedObj.ToJSON(False));
finally
ClonedObj.Free;
end;
finally
Obj.Free;
end;
end;
{
"foo": [
"bar",
{},
null,
true,
false,
{
"key": "value"
}
]
}
05-02 17:27