Delphi XE5 JSON嵌套对象

时间:2015-11-10 09:58:45

标签: json delphi delphi-xe5

我正在尝试生成JSON,数据源是DB FireBird。 我有一个架构来生成这样的路径:

schema:= TDictionary<string, string>.Create;
  schema.Add('DBTableName1', 'nest1.valueKeyName1');
  schema.Add('DBTableName2', 'nest1.valueKeyName2');
  schema.Add('DBTableName3', 'nest2.valueKeyName1');
  schema.Add('DBTableName4', 'nest3.valueKeyName1');
  schema.Add('DBTableName5', 'nest3.valueKeyName2');
  schema.Add('DBTableName6', 'nest4.valueKeyName1');

如何创建生成路径以创建嵌套对象的函数?

{
    "nest1": {
        valueKeyName1: DBTableValue1,
        valueKeyName2: DBTableValue2,
    },
    "nest2": {
        valueKeyName1: DBTableValue3 
    },
    "nest5":{
        "nest6": {
            "key1": "value1",
            "key2": "value2",
        },
    "nest7": {}
}

在JavaScript中我可以做类似的事情:

if (object.hasOwnProperty['key'] == false) object['key'] = {};
object = object['key'];

但是在Delphi中我遇到了问题,并且不知道如何更深入:

function TKlient.wprowadzWartoscDoJSON(wartosc: string; JSON: TJSONObject; sciezka: TStringList): TJSONObject;
var
  i: integer;
  obiekt: TJSONObject;
  para: TJSONPair;
  zagniezdzen: integer;

begin
  zagniezdzen := sciezka.Count - 2;
  obiekt := JSON;
  para:= obiekt.Get(sciezka[i]);

  for i := 1 to zagniezdzen do
  begin
    if obiekt.Get(sciezka[i]) = nil then obiekt.AddPair(sciezka[i], TJSONObject.Create)
    else obiekt := obiekt.Get(sciezka[i]);
  end;

  obiekt.AddPair(sciezka[sciezka.Count - 1], wartosc);

  result := obiekt;
end;

1 个答案:

答案 0 :(得分:1)

这里我使用TDictionary<string, TJSONObject>来跟踪对象路径。

var
  schema: TDictionary<string, string>;
  pathSchema: TDictionary<string{path to every object}, TJSONObject>;
  pair: TPair<string, string>;
  values: TStringDynArray;
  jsonObj,
  jsonChildObj,
  jsonParentObj: TJSONObject;
  path: string;
  i: Integer;

begin
  schema := TDictionary<string, string>.Create;
  try
    schema.Add('DBTableName1', 'nest1.valueKeyName1');
    schema.Add('DBTableName2', 'nest1.valueKeyName2');
    schema.Add('DBTableName3', 'nest2.valueKeyName1');
    schema.Add('DBTableName4', 'nest3.valueKeyName1');
    schema.Add('DBTableName5', 'nest3.valueKeyName2');
    schema.Add('DBTableName6', 'nest4.valueKeyName1');
    schema.Add('value1',       'nest5.nest6.key1');
    schema.Add('value2',       'nest5.nest6.key2');

    pathSchema := TDictionary<string, TJSONObject>.Create;
    try

      jsonObj := TJSONObject.Create;
      try
        for pair in schema do begin
          values := SplitString(pair.Value, '.');
          path := '';
          jsonParentObj := jsonObj;
          for i := Low(values) to High(values)-1 do begin
            if i > 0 then
              path := path + '.';
            path := path + values[i];
            if pathSchema.ContainsKey(path) then
              jsonChildObj := pathSchema[path]
            else begin
              jsonChildObj := TJSONObject.Create;
              jsonParentObj.AddPair(TJSONPair.Create(values[i], jsonChildObj));
              pathSchema.Add(path, jsonChildObj);
            end;
            jsonParentObj := jsonChildObj;
          end;
          jsonChildObj.AddPair(TJSONPair.Create(values[High(values)], pair.Key));
        end;

        WriteLn(jsonObj.ToString);

      finally
        jsonObj.Free;
      end;

    finally
      pathSchema.Free;
    end;

  finally
    schema.Free;
  end;

  ReadLn;

end.

以上内容如下:

{
    "nest4":{
        "valueKeyName1":"DBTableName6"
    },
    "nest2":{
        "valueKeyName1":"DBTableName3"
    },
    "nest5":{
        "nest6":{
            "key1":"value1",
            "key2":"value2"
        }
    },
    "nest1":{
        "valueKeyName1":"DBTableName1",
        "valueKeyName2":"DBTableName2"
    },
    "nest3":{
        "valueKeyName1":"DBTableName4",
        "valueKeyName2":"DBTableName5"
    }
}