我想用angularjs來實現問卷,所以我有下面的響應數組,我需要將這個對象數組轉換爲json格式,就像下面的輸出那樣,我怎樣才能將對象數組轉換爲json格式?AngularJS將json格式轉換成另一種json格式
var response=[{"questiongroup":1,"question":1,"response":"response1"},
{"questiongroup":1,"question":2,"response":"response2"},
{"questiongroup":1,"question":3,"response":"response3"},
{"questiongroup":1,"question":4,"response":"response4"},
{"questiongroup":1,"question":5,"response":"response5"},
{"questiongroup":2,"question":6,"response":"response6"},
{"questiongroup":2,"question":7,"response":"response7"},
{"questiongroup":2,"question":8,"response":"response8"},
{"questiongroup":2,"question":9,"response":"response9"},
{"questiongroup":2,"question":10,"response":"response10"}];
所需的輸出
{"questiongroups": [
{
"questiongroup": 1,
"question": [
{
"question": 1,
"response":"response1"
},
{
"question": 2,
"response":"response2"
},
{
"question": 3,
"response":"response3"
},
{
"question": 4,
"response":"response4"
},
{
"question": 5,
"response":"response5"
}]
},
{
"questiongroup": 2,
"question": [
{
"question": 6,
"response":"response6"
},
{
"question": 7,
"response":"response7"
},
{
"question": 8,
"response":"response8"
},
{
"question": 9,
"response":"response9"
},
{
"question": 10,
"response":"response10"
}]
}
]
}
對不起,我是在這個標記爲重複的太倉促 - 看來,你不問如何到這個對象到JSON轉換,你問如何重新安排數據轉換成不同的結構。 –
是的,它不同我需要更改格式。 –
你嘗試過什麼嗎?像這樣的數據突變非常普遍。 –