I'm using the JSON superobject library to save objects to a file. When my code worked, they were written with proper formatting.
procedure TDCell.Save(fileName: string);
var i,j : integer;
JsonObjCol1, JsonObjCol2, JsonObjCol3, JsonObjCol4: ISuperObject;
begin
JsonArray := SO();
JsonObjCol1 := SO();
JsonObjCol2 := SO();
JsonObjCol3 := SO();
JsonObjCol4 := SO();
for i := 0 to Table.ColCount - 1 do
begin
for j := 0 to Table.RowCount - 1 do
begin
if Table.Objects[i, j] is TEdit then
with Table.Objects[i, j] as TEdit do
case i of
0:JsonArray[Name] := SO(Table.Cells[i, j]);
1:JsonObjCol1[Name] := SO(Table.Cells[i, j]);
2:JsonObjCol2[Name] := SO(Table.Cells[i, j]);
3:JsonObjCol3[Name] := SO(Table.Cells[i, j]);
4:JsonObjCol4[Name] := SO(Table.Cells[i, j]);
end
else
if Table.Objects[i, j] is TLabel then
with Table.Objects[i, j] as TLabel do
case i of
0:JsonArray[Name] := SO(Caption);
1:JsonObjCol1[Name] := SO(Caption);
2:JsonObjCol2[Name] := SO(Caption);
3:JsonObjCol3[Name] := SO(Caption);
4:JsonObjCol4[Name] := SO(Caption);
end
end;
case i of
0:JsonObject['Col' + IntToStr(Table.ColCount - 1 - i)] := JsonArray;
1:JsonObject['Col' + IntToStr(Table.ColCount - 1 - i)] := JsonObjCol1;
2:JsonObject['Col' + IntToStr(Table.ColCount - 1 - i)] := JsonObjCol2;
3:JsonObject['Col' + IntToStr(Table.ColCount - 1 - i)] := JsonObjCol3;
4:JsonObject['Col' + IntToStr(Table.ColCount - 1 - i)] := JsonObjCol4;
end;
end;
JsonObject.SaveTo(fileName, true);
end;
JsonArray
also has type ISuperObject
But it seems that there is some opposite order sorting feature running in this library. Not only objects but also key-value pairs in them are written in opposite alphabetical order.
{
"Col4": {
"Label03": "Hello03",
"Label01": "Hello01",
"Edit04": "Hello04",
"Edit02": "Hello02",
"Edit00": "Hello00"
},
"Col3": {
"Label13": "Hello13",
"Label11": "Hello11",
"Edit14": "Hello14",
"Edit12": "Hello12",
"Edit10": "Hello10"
},
"Col2": {
"Label23": "Hello23",
"Label21": "Hello21",
"Edit24": "Hello24",
"Edit22": "Hello22",
"Edit20": "Hello20"
},
"Col1": {
"Label33": "Hello33",
"Label31": "Hello31",
"Edit34": "Hello34",
"Edit32": "Hello32",
"Edit30": "Hello30"
},
"Col0": {
"Label43": "Hello43",
"Label41": "Hello41",
"Edit44": "Hello44",
"Edit42": "Hello42",
"Edit40": "Hello40"
}
}
What should I do to make superobject write the file in the proper order? I've tested that objects are added in the proper order in cycles. The file must look like this.
{
"Col0": {
"Edit00": "Hello00",
"Label01": "Hello01",
"Edit02": "Hello02",
"Label03": "Hello03",
"Edit04": "Hello04"
},
"Col1": {
"Edit10": "Hello10",
"Label11": "Hello11",
"Edit12": "Hello12",
"Label13": "Hello13",
"Edit14": "Hello14"
},
and so on
There is no proper order. The elements of a JSON object can be presented in any order, and the JSON standard makes it clear that the meaning of the file cannot depend on the order of the elements of a JSON object:
An object is an unordered set of name/value pairs.
If you require ordered data then you need to use a JSON array:
An array is an ordered collection of values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.