Delphi JSON superobject按字母顺序将多个对象保存到文件中

时间:2015-04-08 17:06:15

标签: json delphi superobject

我正在使用JSON超级项目库将对象保存到文件中。当我的代码工作时,它们是用适当的格式编写的。

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也有ISuperObject

类型

但似乎在这个库中运行了一些相反的顺序排序功能。不仅对象而且其中的键值对以相反的字母顺序写入。

{
 "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"
 }
}

如何让superobject以正确的顺序写入文件?我已经测试过,对象是以正确的顺序循环添加的。该文件必须如下所示。

{
 "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

1 个答案:

答案 0 :(得分:0)

没有正确的订单。 JSON对象的元素可以按任何顺序显示,JSON standard清楚地表明文件的含义不依赖于JSON对象元素的顺序:

  

对象是一组无序的名称/值对。

如果您需要有序数据,则需要使用JSON数组:

  

数组是有序的值集合。