json - 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([])
?
解决方案
感谢您的回答,我试试这个:
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;
可以了,谢谢
推荐阅读
- python-3.x - 如何在没有 GPU 的 16GB RAM 的计算机上读取大约 50GB 文件(CSV,txt)的大数据
- c# - 禁用敌人,获取随机数,传送到该数字作为坐标,启用敌人
- reactjs - 使用钩子将类组件转换为函数式
- c# - Unity 输入系统鼠标按键按住并拖动
- javascript - 如何解决屏幕缩小时出现的内容重定位问题?
- css - ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。在 mat-tab-group 上使用 Ngclass
- r - 以 yymm 格式将月份添加到日期
- sql - 当 unpivot 未检测到具有不同类型的字段时,雪花如何转换选择查询的所有字段?
- java - 使用信号量开发同步算法
- python - 多次运行脚本时出现 Python 错误:模块“__main__”没有属性“__spec__”