我有两个javascript对象。找到两个对象:
var tableRespondent = [{Id: 1, name: "abc", mail: "abc@gmail.com"}];
var tableQuestionnaire = [{Id: 1, externalId: "Q1", text: "Food Questionnaire"}, {Id: 2, externalId: "Q2", text: "Pet Questionnaire"}];
现在我需要将它们组合成以下格式:
var output = {Respondent: [{Id: 1, name: "abc", mail: "abc@gmail.com"}], Questionnaire: [{Id: 1, externalId: "Q1", text: "Food Questionnaire"},{Id: 2, externalId: "Q2", text: "Pet Questionnaire"}] };
然后我需要将其转换为JSON,如下所示:
var outputJson = var output = {"Respondent": [{"Id": 1, "name": "abc", "mail": "abc@gmail.com"}], Questionnaire: [{"Id": 1, "externalId": "Q1", "text": "Food Questionnaire"}, {"Id": 2, "externalId": "Q2", "text": "Pet Questionnaire"}] };
应该如何实现?我对javascript很新。
答案 0 :(得分:1)
以下代码可以帮助您
var output = {};
var tableRespondent = [{Id: 1, name: "abc", mail: "abc@gmail.com"}];
var tableQuestionnaire = [{Id: 1, externalId: "Q1", text: "Food Questionnaire"}, {Id: 2, externalId: "Q2", text: "Pet Questionnaire"}];
output['tableRespondent'] = tableRespondent;
output['tableQuestionnaire'] = tableQuestionnaire;
output = JSON.stringify(output);
console.log(output);
//Json string
console.log(typeof output);