Javascript 如何从json服务器将对象数组存储在db.json中
我正在使用json服务器创建一个假后端,以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?",
{
"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服务器时,您面临的确切问题是什么吗?