如何从 json-server 将对象数组存储在 db.json 中

Posted

技术标签:

【中文标题】如何从 json-server 将对象数组存储在 db.json 中【英文标题】:How to store array of objects in db.json from json-server 【发布时间】:2017-08-27 07:36:12 【问题描述】:

我正在使用 json-server 创建一个假后端来以 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]
                    
                ]
            
        ]
    

但是当我对 json-server 执行 jQuery POST 时,json 数据存储如下 -


  "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-server 中?

【问题讨论】:

您应该使用JSON.stringify(serverResponse) 正确保存 JSON 对象,或者使用此 url (scotch.io/tutorials/…) 来获得有关如何在 JSON-Server 中存储数据的帮助 在 db.json- "\"quizName\":\"Which Harry Potter Character Are You?\",\"characters\":[\"harry\",\"ron\",\"harmione\"],\"qa\":[\"question\":\"do u play chess?\",\"answers\":[\"name\":\"yes\",\"charscore\":[0,1,0],\"name\":\"no\",\"charscore\":[1,0,1]],\"question\":\"do u have scar?\",\"answers\":[\"name\":\"yes\",\"charscore\":[1,0,0],\"name\":\"no\",\"charscore\":[0,1,1]],\"question\":\"du u spell magic?\",\"answers\":[\"name\":\"yes\",\"charscore\":[0,0,1],\"name\":\"no\",\"charscore\":[1,1,0]]]": "", "id": 1 字符串化在这里没有帮助 是的,使用stringify 存储任何JSON 对象是正确的方法,当您想将其用作JSON 对象时,您可以使用JSON.parse(serverResponse) 您能告诉我,您在保存到 json-server 时遇到的具体问题是什么? 【参考方案1】:

修改 AJAX 调用以发送 stringify 对象并设置 contentType: "application/json" 就可以了

修改AJAX调用如下-

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

【讨论】:

以上是关于如何从 json-server 将对象数组存储在 db.json 中的主要内容,如果未能解决你的问题,请参考以下文章

Json-server 错误:数据必须是对象。需要将 JSON 数组更改为 JSON 对象

如何将数据从网站保存到 json-server

如何动态启动 json-server

如何将分页应用于 json-server 中的子对象?

不支持 user.json 中的“id”(数字)类型。使用 json-server 时使用对象或对象数组出错,为啥?

如何将对象列表存储在数组中?