[英]Stringify in objects JSON inside a object JSON
This is my object JSON: 这是我的对象JSON:
var myJSon = {
"Student": "name",
"Answers":{
"Answer1": {
"question": "question",
"answer": "black",
"time": "00:02:30",
"number_tentatives": "1"
},
"Answer2": {
"question": "question",
"answer": "black",
"time": "00:02:30",
"number_tentatives": "2"
}
}
};
I need to fill in the object "Answer1" or "Answer2". 我需要填写对象“ Answer1”或“ Answer2”。 I tried
我试过了
myJSon.Respostas = JSON.stringify("One","hello","00:03:22","1");
But this results in {Student":"\\"name\\"","Answers":"\\"oi\\"}
但这导致
{Student":"\\"name\\"","Answers":"\\"oi\\"}
What I would like is {"Student": "\\"name\\"", "Answers": {"Answer1": {"question": "One", "answer": "hello" ,"time":"00:03:22" ,"number_tentatives": "1"}, "
我想要的是
{"Student": "\\"name\\"", "Answers": {"Answer1": {"question": "One", "answer": "hello" ,"time":"00:03:22" ,"number_tentatives": "1"}, "
If you have an object containing multiple answers, it should be an array or map of answers. 如果您的对象包含多个答案,则它应该是答案的数组或映射。
Let's think of your object's initial state as this: 让我们考虑一下对象的初始状态:
var myJson = {student: 'Student Name', answers: []};
So then you could start filling the answers array like: 因此,您可以像下面这样开始填写答案数组:
myJson.answers.push({question: 'q', answer: 'a', time: 1, number_tentatives: 1});
If you'd now access myJson.answers
it would be an array with one answer in it. 如果现在访问
myJson.answers
,它将是一个包含一个答案的数组。
If you still think the way to go would be objects (so a 'key' is assigned to each answer), you would do this, instead of push
: 如果您仍然认为要走的路是对象(因此为每个答案分配了一个“键”),那么您可以这样做,而不是
push
:
myJson.answers['answer1'] = {question: 'q', answer: 'a', time: 1, number_tentatives: 1};
If you want to add additional data, then you could try this: 如果要添加其他数据,则可以尝试以下操作:
myJSon.Answers.Answer3 ={"question":"One","answer":"hello","time":"00:03:22","number_tentatives":"1"};
then test it like 然后像测试
console.log(JSON.stringify(myJSon));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.