首页 > 解决方案 > JSON - ISuperObject

问题描述

我想使用 ISuperObject 创建我的 JSON 文件,但我需要知道如何将一个对象添加到另一个对象中。例如,这是我的代码:

aJSONChannel := SO;

for J := 0 to ListTest.Count - 1 do
begin
  aJSONChannel.S['a'] := ListTest[j].Code;
  aJSONChannel.S['b'] := ListTest[j].Valeur;

  // Create node "tranlsations"
  aJSONChannel.O['translations'] := SA([]);

  for I := 0 to ListTest[j].ListTranslation.Count-1 do
  begin
    aJSONTransaltionsLang := SO;
    aJSONTransaltionsLang.S['title'] := ListTest[j].ListTranslation[i].Title;

    aJSONChannel.A['translations'].Add(aJSONTransaltions);
  end;

结果:

{
"b": "valeur",
"a": "code",
"translations": [
       {"title" : "fr"},
       {"title" : "en"},
       {"title" : "de"}, 
       {"title" : "it"}
     ],
}

但我想要这个:

{
"b": "valeur",
"a": "code",
"translations": {
       "fr" : {"title" : "fr"},
       "en" : {"title" : "en"},
       "de" : {"title" : "de"}, 
       "it" : {"title" : "it"}
                },
  }

我使用SA([]),但我不想创建一个数组,只是另一个对象中的一个对象。

我尝试使用SO([])而不是SA([]),但我怎样才能像我们一样向对象添加“节点” SA([])

标签: jsondelphidelphi-xe

解决方案


感谢您的回答,我试试这个:

aJSONChannel := SO;

aJSONChannel.S['a'] := ListTest[j].Code;
aJSONChannel.S['b'] := ListTest[j].Valeur;

aJSONTransaltions := SO;
aJSONTransaltionsLang := SO;
for I := 0 to ListTest[j].ListTranslation.Count-1 do
begin

   aJSONTransaltionsLang.S['title'] := ListTest[j].ListTranslation[i].Title;


   aJSONTransaltions.O[ListTest[j].ListTranslation[i].LANG] := aJSONTransaltionsLang;

   aJSONChannel.O['translations'] = aJSONTransaltions;
 end;

结构很好,但 aJSONTransaltionsLang.S['title'] 采用所有出现的最后出现的值,如下所示:

{
 "b": "valeur",
 "a": "code",
  "translations": {
         "fr" : {"title" : "it"},
         "en" : {"title" : "it"},
         "de" : {"title" : "it"}, 
         "it" : {"title" : "it"}
                  },
  }

编辑 :

 aJSONChannel := SO;

 aJSONChannel.S['a'] := ListTest[j].Code;
 aJSONChannel.S['b'] := ListTest[j].Valeur;

 aJSONTransaltions := SO;
 for I := 0 to ListTest[j].ListTranslation.Count-1 do
 begin
    aJSONTransaltionsLang := SO;
    aJSONTransaltionsLang.S['title'] := ListTest[j].ListTranslation[i].Title;


    aJSONTransaltions.O[ListTest[j].ListTranslation[i].LANG] :=      aJSONTransaltionsLang;

    aJSONChannel.O['translations'] = aJSONTransaltions;
  end;

可以了,谢谢


推荐阅读