ElRoro
ElRoro

Reputation: 213

JSON - ISuperObject

I want to create my JSON file using ISuperObject, but I need to know how to add an Object into another Object. For example, this is my code:

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;

Result:

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

But I want this:

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

I use SA([]), but I don't want to create an array, just an object in another object.

I try to use SO([]) and not SA([]), but how can I add a "node" to an object like we can with SA([])?

Upvotes: 2

Views: 542

Answers (1)

ElRoro
ElRoro

Reputation: 213

Thanks for your answer, i try this :

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;

The structure is good but aJSONTransaltionsLang.S['title'] take the value of last occurence for all occurence like this :

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

EDIT :

 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;

It's works, thanks

Upvotes: 2

Related Questions