在对象JSON中的对象JSON中进行字符串化

时间:2017-11-21 20:18:23

标签: javascript 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"
        }
    }
};

我需要填写对象“Answer1”或“Answer2”。我试过了

myJSon.Respostas = JSON.stringify("One","hello","00:03:22","1");

但这导致{Student":"\"name\"","Answers":"\"oi\"}

我想要的是{"Student": "\"name\"", "Answers": {"Answer1": {"question": "One", "answer": "hello" ,"time":"00:03:22" ,"number_tentatives": "1"}, "

2 个答案:

答案 0 :(得分:1)

如果您有一个包含多个答案的对象,则它应该是一个数组或答案地图。

让我们想一下你对象的初始状态:

var myJson = {student: 'Student Name', answers: []};

那么你可以开始填写答案数组,如:

myJson.answers.push({question: 'q', answer: 'a', time: 1, number_tentatives: 1});

如果你现在访问myJson.answers,那么它就是一个包含一个答案的数组。

如果你仍然认为要走的路是对象(所以每个答案分配了一个'键'),你会这样做,而不是push

myJson.answers['answer1'] = {question: 'q', answer: 'a', time: 1, number_tentatives: 1};

答案 1 :(得分:0)

如果您想添加其他数据,可以试试这个:

dispatch

然后测试它

dispatch()