Javascript 将JSON转换为具有其他结构的新JSON
我有一个表示树图的JSON,并希望将其转换为具有新结构的JSON,该结构可以通过treant.js可视化 它应该使用不同数量的节点。每个节点/子节点可以有0或2个子节点。这实际上是我的主要问题,如何迭代输入JSON以获取所有子项和子项,而不必为每个子项创建循环 此外,父节点的标题仅在其子节点中表示,但我需要在父节点本身中表示 输入:Javascript 将JSON转换为具有其他结构的新JSON,javascript,json,Javascript,Json,我有一个表示树图的JSON,并希望将其转换为具有新结构的JSON,该结构可以通过treant.js可视化 它应该使用不同数量的节点。每个节点/子节点可以有0或2个子节点。这实际上是我的主要问题,如何迭代输入JSON以获取所有子项和子项,而不必为每个子项创建循环 此外,父节点的标题仅在其子节点中表示,但我需要在父节点本身中表示 输入: treeModel = { "TreeModel": { "AlgorithmName": "CART", "Child": [{
treeModel = {
"TreeModel": {
"AlgorithmName": "CART",
"Child": [{
"RecordCount": 47,
"Score": "1", //should be the name
"ScoreDistribution": [
[24, 23], //should be the description as string
[0.5106382978723404, 0.4893617021276596]
],
"TreeNodePredicate": {
"FieldPredID": 0,
"FieldPredName": "Attribute1", //should be the title from parent
"FieldValue": 0,
"Operator": "EQUAL",
"PredicateType": "SimplePredicate"
}
}, {
"RecordCount": 214,
"Score": "0", //should be the name
"ScoreDistribution": [
[2, 212], //should be the description as string
[0.009345794392523364, 0.9906542056074767]
],
"TreeNodePredicate": {
"FieldPredID": 0,
"FieldPredName": "Attribute1", //should be the title from parent
"FieldValue": 1,
"Operator": "EQUAL",
"PredicateType": "SimplePredicate"
}
}],
"RecordCount": 261,
"Score": "0", //should be the name
"ScoreDistribution": [
[26, 235], //should be the description as string
[0.09961685823754787, 0.9003831417624522]
],
"TreeNodePredicate": {
"PredicateType": "TruePredicate"
}
}
}
输出:
chart_config = {
chart: {
container: "#tree-simple"
},
nodeStructure: {
text: {
name: "0" , //Score
title: "Attribute1", //FieldPredName from Child
desc: "26, 235", //ScoreDistribution as String
},
children: [
{
text: {
name: "1" , //Score
title: "", //FieldPredName from Child(empty as no childs)
desc: "24, 23", //ScoreDistribution as String
}
},
{
text: {
name: "0" , //Score
title: "", //FieldPredName from Child(empty as no childs
desc: "2, 212", //ScoreDistribution as String
}
}
]
}
};
您可以使用递归和迭代方法进行转换
函数转换(源代码){
变量目标={
正文:{
姓名:来源。得分,
标题:source.Child&&source.Child[0]&&source.Child[0]。TreeNodePredicate&&source.Child[0]。TreeNodePredicate.FieldPredName | |“”,
desc:source.ScoreDistribution[0]。加入(“,”)
}
};
if(Array.isArray(source.Child)){
target.children=source.Child.map(转换);
}
回报目标;
}
var treeModel={treeModel:{AlgorithmName:“CART”,Child:[{RecordCount:47,分数:“1”,分数分布:[[24,23],[0.5106382978723404,0.4893617021276596]],TreeNodePredicate:{FieldPredID:0,FieldPredName:“Attribute1”,FieldValue:0,运算符:“EQUAL”,谓词类型:“SimplePredicate”},{RecordCount:214,分数:“0”,ScoreDistribution:[[2,212],[0.009345794392523364,0.9906542506074767],[TreeNodeDistribution:{FieldPredID:0,FieldPredName:“Attribute1”,FieldValue:1,运算符:“EQUAL”,谓词类型:“SimplePredicate”},记录计数:261,分数:“0”,ScoreDistribution:[26,235],[0.09961685823754787,0.9003831417624522],[TreeNodeDistribute:{PredicateType:“TruePredicate”}}},
chartu config={chart:{container:“#tree simple”},节点结构:convert(treeModel.treeModel)};
console.log(图表配置);
。作为控制台包装{max height:100%!important;top:0;}
这不是代码编写服务,请尝试自己解决问题,遇到问题时返回。将其传递给重新分配值的构造函数。简单。