我有兩個json對象具有相似的鍵question
。連接兩個相同的密鑰json對象
var data = {"question":[{
QuestionID : counter1,
QuestionText: question1,
Choices:[{ChoiceID:100,Choice:"Yes",NextQuestionID:counter},
{ChoiceID:101,Choice:"No",NextQuestionID:counter}],
AnswerType: answer_type
}]};
var data1 = {"question":[{
QuestionID : counter2,
QuestionText: question2,
Choices:[{ChoiceID:103,Choice:"Yes",NextQuestionID:counter},
{ChoiceID:105,Choice:"No",NextQuestionID:counter}],
AnswerType: answer_type
}]};
我想他們concate與鍵「問題」一個JSON對象和值將是數組像下面
var final = {"question":[
{
QuestionID : counter1,
QuestionText: question1,
Choices:[{ChoiceID:100,Choice:"Yes",NextQuestionID:counter},
{ChoiceID:101,Choice:"No",NextQuestionID:counter}],
AnswerType: answer_type
},
{
QuestionID : counter2,
QuestionText: question2,
Choices:[{ChoiceID:103,Choice:"Yes",NextQuestionID:counter},
{ChoiceID:105,Choice:"No",NextQuestionID:counter}],
AnswerType: answer_type
}
]};
我嘗試過很多辦法及以下的方式接近我的目標,但外幣陣列數據DATA1和對象的
var jsons = new Array();
jsons.push(data);
jsons.push(data1);
我的問題將會解決如果我可以concate question:Object
和question:Array[2]
其中每個索引包含對象。最終的輸出將是question:Array[3]
任何幫助將不勝感激。 在此先感謝。
這是JavaScript的,而不是JSON解決了這個問題。另外,請描述你已經嘗試了什麼,哪些不起作用。 – 2014-02-13 06:34:45
如果它只是這兩個,你需要{'問題':[q1,q2]}只是做一個'var myObject = {'question':[data.question,data1.question]};' – algrebe