我正在使用DHTMLX的TreeGrid JavaScript控件来允许用户输入和排列嵌套数据。该控件只允许通过平行访问TreeGrid中的数据(你认为在这个时代它会做JSON ......)。这些行包含rowId,rowIndex,parentId,hasChildren,当然还有数据本身。
数据输入如下:
我需要输出(稍后从Mongo读回来)嵌套的JSON,如下所示。
我已经尝试过迭代和递归。我更喜欢递归地执行它,但是我正忙着向积累JSON的对象添加新节点,因为属性名称在整个数据结构中重复,使得obj [key] =(这里是recurse)看似不可能。我需要保留订单。这可能很简单,我错过了一些东西。任何帮助将不胜感激。
{
"NpcResponses": [{
"Challenge": "Hello there traveler!",
"PlayerResponses": [{
"Keywords": ["hello","hi"],
"NpcResponses": [{
"Challenge": "Would you [mind] doing something for me?",
"PlayerResponses": [{
"Keywords": ["mind","sure","ok"],
"NpcResponses": [{
"Challenge": "Please [take] this bag of sugar to the stable master.",
"PlayerResponses": [{
"Keywords": ["ok","take","yes"],
"NpcResponses": [{
"Challenge": "Thank you, you are most kind.",
"Event": "QST_BagOfSugar01"
}]
},
{
"Keywords": ["no"],
"NpcResponses": [{
"SetMood": "Disappointed",
"Challenge": "Oh well, have a nice day."
}]
}]
}]
},
{
"Keywords": ["no"],
"NpcResponses": [{
"Event": "Set Mood Disappointed",
"Challenge": "Never mind, I'll find someone else to help me."
}]
}]
}]
}]
}]
}