Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从json服务器将对象数组存储在db.json中_Javascript_Jquery_Json_Json Server - Fatal编程技术网

Javascript 如何从json服务器将对象数组存储在db.json中

Javascript 如何从json服务器将对象数组存储在db.json中,javascript,jquery,json,json-server,Javascript,Jquery,Json,Json Server,我正在使用json服务器创建一个假后端,以json格式存储一些数据。数据复杂,如下所示: { "id": 0, "quizName": "Which Harry Potter Character Are You?", "characters": ["Harry", "Ron", "Hermoine"], "qa": [{ "question": "Do you play Chess?",

我正在使用json服务器创建一个假后端,以json格式存储一些数据。数据复杂,如下所示:

   {
        "id": 0,
        "quizName": "Which Harry Potter Character Are You?",
        "characters": ["Harry", "Ron", "Hermoine"],
        "qa": [{
                "question": "Do you play Chess?",
                "answers": [{
                        "name": "yes",
                        "charscore": [0, 1, 0]
                    },
                    {
                        "name": "no",
                        "charscore": [1, 0, 1]
                    }
                ]
            },
            {
                "question": "Do you spell magic correctly?",
                "answers": [{
                        "name": "yes",
                        "charscore": [0, 0, 1]
                    },
                    {
                        "name": "no",
                        "charscore": [1, 1, 0]
                    }
                ]
            },
            {
                "question": "Do you have a Scar?",
                "answers": [{
                        "name": "yes",
                        "charscore": [1, 0, 0]
                    },
                    {
                        "name": "no",
                        "charscore": [0, 1, 1]
                    }
                ]
            }
        ]
    }
{
  "quizName": "Which Harry Potter Character Are You?",
  "characters[]": [
    "Harry",
    "Ron",
    "Hermoine"
  ],
  "qa[0][question]": "Do you play Chess?",
  "qa[0][answers][0][name]": "yes",
  "qa[0][answers][0][charscore][]": [
    "0",
    "1",
    "0"
  ],
  "qa[0][answers][1][name]": "no",
  "qa[0][answers][1][charscore][]": [
    "1",
    "0",
    "1"
  ],
  "qa[1][question]": "Do you spell magic correctly?",
  "qa[1][answers][0][name]": "yes",
  "qa[1][answers][0][charscore][]": [
    "0",
    "0",
    "1"
  ],
  "qa[1][answers][1][name]": "no",
  "qa[1][answers][1][charscore][]": [
    "1",
    "1",
    "0"
  ],
  "qa[2][question]": "Do you have a Scar?",
  "qa[2][answers][0][name]": "yes",
  "qa[2][answers][0][charscore][]": [
    "1",
    "0",
    "0"
  ],
  "qa[2][answers][1][name]": "no",
  "qa[2][answers][1][charscore][]": [
    "0",
    "1",
    "1"
  ],
  "id": 1
}
但当我将jQuery发布到json服务器时,json数据存储如下-

   {
        "id": 0,
        "quizName": "Which Harry Potter Character Are You?",
        "characters": ["Harry", "Ron", "Hermoine"],
        "qa": [{
                "question": "Do you play Chess?",
                "answers": [{
                        "name": "yes",
                        "charscore": [0, 1, 0]
                    },
                    {
                        "name": "no",
                        "charscore": [1, 0, 1]
                    }
                ]
            },
            {
                "question": "Do you spell magic correctly?",
                "answers": [{
                        "name": "yes",
                        "charscore": [0, 0, 1]
                    },
                    {
                        "name": "no",
                        "charscore": [1, 1, 0]
                    }
                ]
            },
            {
                "question": "Do you have a Scar?",
                "answers": [{
                        "name": "yes",
                        "charscore": [1, 0, 0]
                    },
                    {
                        "name": "no",
                        "charscore": [0, 1, 1]
                    }
                ]
            }
        ]
    }
{
  "quizName": "Which Harry Potter Character Are You?",
  "characters[]": [
    "Harry",
    "Ron",
    "Hermoine"
  ],
  "qa[0][question]": "Do you play Chess?",
  "qa[0][answers][0][name]": "yes",
  "qa[0][answers][0][charscore][]": [
    "0",
    "1",
    "0"
  ],
  "qa[0][answers][1][name]": "no",
  "qa[0][answers][1][charscore][]": [
    "1",
    "0",
    "1"
  ],
  "qa[1][question]": "Do you spell magic correctly?",
  "qa[1][answers][0][name]": "yes",
  "qa[1][answers][0][charscore][]": [
    "0",
    "0",
    "1"
  ],
  "qa[1][answers][1][name]": "no",
  "qa[1][answers][1][charscore][]": [
    "1",
    "1",
    "0"
  ],
  "qa[2][question]": "Do you have a Scar?",
  "qa[2][answers][0][name]": "yes",
  "qa[2][answers][0][charscore][]": [
    "1",
    "0",
    "0"
  ],
  "qa[2][answers][1][name]": "no",
  "qa[2][answers][1][charscore][]": [
    "0",
    "1",
    "1"
  ],
  "id": 1
}
post方法如下所示-

$.post(this.serverUrl, val, function (serverResponse) {
      //val contains the expected javascript object
      console.log(serverResponse);
});

如何在json服务器中正确存储这些值?

修改AJAX调用以发送stringify对象,并设置
contentType:“application/json”
成功了

修改了AJAX调用,如下所示-

$.ajax({
      type: "POST",
      url: this.serverUrl,
      data: JSON.stringify(val),
      contentType: "application/json"
 });

您应该使用
JSON.stringify(serverResponse)
来正确保存JSON对象,或者使用此url()来获取一些关于如何在JSON服务器中存储数据的帮助stringify对象在db.JSON中发布以下数据-
{{{{“quizName\”:\“你是哪个哈利波特角色?\”,“characters\:[\“Harry\”,“ron\”,“harmione\”,“qa\:[{”“答案”是以下几种::[{“名称”姓名:“,,,,,“答案”回答:”:[{{“名称”姓名姓名::[0,1,1,1,0 0[0 0.1.0],,,{“名称”姓名::“你们们会玩国际象象棋吗?”提问:,,,,“答案”回答:::[{{“名称”姓名姓名::::[0,1,1,1,1,1,1,1,1,1,1,0.0.0 0 0[[0.0[0.0[0.0.0.0],,,,,,:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“\'name\':\'yes\',\'charscore\':[0,0,1]},{\“name\”:\“no\”,\“charscore\”:[1,1,0]}]}]:“,“id”:1}
字符串化在这里没有帮助是的,使用
stringify
存储任何JSON对象是正确的方法,当您想将其用作JSON对象时,可以使用
JSON.parse(serverResponse)
您能告诉我,在保存到json服务器时,您面临的确切问题是什么吗?