/*************************** *如何循环8次,生成如下JSON结构 ****************************/ var labels = ["R","A","B","C","D","E","F","G"]; var JSON_DATA = new Object(); var ARR_1 = new Array(); var ARR_2 = new Array(); //To do? //... JSON_DATA["x"] = ARR_1; JSON_DATA["y"] = ARR_2; /*************************** *希望生成后的JSON结构 ****************************/ JSON_DATA = { "x":[{ nIndex:0, name:"R", basic:{ R1:"", R2:"", R3:"", R4:"" },{ nIndex:1, name:"A", basic:{ B1:"", B2:"", B3:"", B4:"" } },{ nIndex:2, name:"B" },{ nIndex:3, name:"C" },{ nIndex:4, name:"D" },{ nIndex:5, name:"E" },{ nIndex:6, name:"F" },{ nIndex:7, name:"G" } ], "y":[{ source:0, target:1 },{ source:0, target:2 },{ source:0, target:3 },{ source:0, target:4 },{ source:0, target:5 },{ source:0, target:6 },{ source:0, target:7 }, ] }
1 var arr=['a','b','c','d']; 2 var obj={}; 3 var arr1=[],arr2=[]; 4 for(var i=0;i<arr.length;i++){ 5 var tmp={}; 6 tmp.nIndex=i; 7 tmp.name=arr[i]; 8 //tmp.basic自行处理 9 arr1.push(tmp); 10 } 11 obj.x=arr1;
输出obj:
{ "x": [ { "nIndex": 0, "name": "a" }, { "nIndex": 1, "name": "b" }, { "nIndex": 2, "name": "c" }, { "nIndex": 3, "name": "d" }, { "nIndex": 0, "name": "a" }, { "nIndex": 1, "name": "b" }, { "nIndex": 2, "name": "c" }, { "nIndex": 3, "name": "d" } ] }
这只是个demo 自己扩展吧
可以先把json结构列好,就可以直接嵌插了
你完美解答了我的问题;
@Coca-code: 哈哈,能解决就好
看了半天没看懂关联
R1:"", R2:"", R3:"", R4:"" //这个后面缺少了一个}
嗯嗯