我有如下JSON对象,我试图通过第一组键对该对象进行分组和推送。
main = {"answer_options":{
"1":{"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"},
"2":{"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}},
"question":
{"1":"The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n","2":"Latitude of a place is the angular distance from<\/p>\r\n"}
};
我试图获得如下输出..任何人都可以帮忙。
{"1" :
{{"1" : "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n"},
{"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"}},
{"2":
{{"2" : "Latitude of a place is the angular distance from<\/p>\r\n"},
{"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}}
}
答案 0 :(得分:1)
您的预期结果不是一个明智的(或常用的)格式。这是替代方法。您可以使用for
/ in
来循环问题,并将其与相应的答案选项相匹配。
main = {
"answer_options": {
"1": {
"1": "optical projection<\/p>\r\n",
"2": "optical mechanism projection<\/p>\r\n",
"3": "mechanical projection<\/p>\r\n",
"4": "all the above<\/p>\r\n"
},
"2": {
"5": "Greenwich to the place<\/p>\r\n",
"6": "equator to the poles<\/p>\r\n",
"7": "equator to the nearer pole<\/p>\r\n",
"8": "equator to the nearer pole along the meridian of the place<\/p>\r\n",
"9": "none of these.<\/p>\r\n"
}
},
"question": {
"1": "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n",
"2": "Latitude of a place is the angular distance from<\/p>\r\n"
}
};
var newMain = {};
for (var key in main.question) {
newMain[ key ] = [
main.question[key],
main.answer_options[key]
];
}
console.log(newMain);