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

Posted

技术标签:

【中文标题】不支持 user.json 中的“id”(数字)类型。使用 json-server 时使用对象或对象数组出错,为啥?【英文标题】:Type of “id” (number) in user.json is not supported. Use objects or arrays of objects error when using json-server, why?不支持 user.json 中的“id”(数字)类型。使用 json-server 时使用对象或对象数组出错,为什么? 【发布时间】:2018-02-22 01:12:43 【问题描述】:

我正在开发一个 Angular 4 应用程序。 我正在尝试从 JSON 文件中获取数据以构建用户仪表板。

我创建了一个 json 文件并尝试使用 JSON 服务器加载它:$ json-server --watch user.json,我收到以下错误:

不支持 user.json 中的“id”(数字)类型。使用对象或对象数组。

当我删除文件中的“id”字段时,我收到与“name”相同的错误。

我觉得我创建 json 文件的方式有问题。我是 Web 开发的新手,所以如果这是一个非常根本的错误,请不要介意。

这是我的 json 文件:


 "id": 1,
 "name":"Kajal Agarwal",
 "department":"Information Technology",
 "college":"Indian Institute of Engineering Science and Technology, Shibpur",
 "subjects":[
    
        "title":"Data Structures",
        "active":true,
        "faculty":"Prasun Ghosal",
        "notifications":4,
        "color":"#9c27b0"
    ,
    
        "title":"Operating System",
        "active":true,
        "faculty":"Niharika Singh",
        "notifications":0,
        "color":"#ffc107"
    ,
    
        "title":"Algorithms",
        "active":true,
        "faculty":"Debojit Mondal",
        "notifications":1,
        "color":"#009688"
    ,
    
        "title":"Web Technologies",
        "active":true,
        "faculty":"Shantanu Saurabh",
        "notifications":2,
        "color":"#ff5722"
    ,
    
        "title":"Formal Language and Automata Theory",
        "active":true,
        "faculty":"Sudhanshu Sekhar",
        "notifications":3,
        "color":"#03a9f4"
    ,
    
        "title":"Digital Logic and Circuit Design",
        "active":false,
        "faculty":"",
        "notifications":0,
        "color":"#9e9e9e"
    
  ]

【问题讨论】:

【参考方案1】:

也许我迟到了,但这只是为未来的读者准备的,因为我也遇到过类似的问题,直到我对 json-server 有了更多的了解。

因此,您的 JSON 对象没有任何问题。您收到该错误的原因与 json-server 的工作方式有关。

直接暴露在 JSON 对象根部的每个键都被视为 json-server 中的单独 URL。在您的情况下,如果您将整个 JSON 对象包装在一个键中,例如:'data',您的错误将得到解决。您的 JSON 对象如下所示


"data":
 "id": 1,
 "name":"Kajal Agarwal",
 "department":"Information Technology",
 "college":"Indian Institute of Engineering Science and Technology, Shibpur",
 "subjects":[
    
        "title":"Data Structures",
        "active":true,
        "faculty":"Prasun Ghosal",
        "notifications":4,
        "color":"#9c27b0"
    ,
    
        "title":"Operating System",
        "active":true,
        "faculty":"Niharika Singh",
        "notifications":0,
        "color":"#ffc107"
    ,
    
        "title":"Algorithms",
        "active":true,
        "faculty":"Debojit Mondal",
        "notifications":1,
        "color":"#009688"
    ,
    
        "title":"Web Technologies",
        "active":true,
        "faculty":"Shantanu Saurabh",
        "notifications":2,
        "color":"#ff5722"
    ,
    
        "title":"Formal Language and Automata Theory",
        "active":true,
        "faculty":"Sudhanshu Sekhar",
        "notifications":3,
        "color":"#03a9f4"
    ,
    
        "title":"Digital Logic and Circuit Design",
        "active":false,
        "faculty":"",
        "notifications":0,
        "color":"#9e9e9e"
    
  ]


现在,如果您尝试从如下 URL 访问根密钥。您将能够获取整个 JSON 对象。

localhost:3000/data

如果您要在 JSON 对象的根目录添加另一个名为“data2”的键并分配其他对象,那么您将拥有另一个 URL,如下所示

localhost:3000/data2

希望这是有道理且有帮助的。 您也可以访问 here 以查看 json-server 工作原理的示例

【讨论】:

【参考方案2】:

那个错误告诉你该怎么做。 使用对象或对象数组

Id 应该是对象或对象数组,其余属性相同。

也许你应该考虑编辑你的 user.json

建议:


    "about" : [
            "id" : 1,
            "name" : "Kajal Agarwal",
            "department" : "Information Technology",
            "college" : "Indian Institute of Engineering Science and Technology, Shibpur"
        
    ],
    "subjects" : [
            "title" : "Data Structures",
            "active" : true,
            "faculty" : "Prasun Ghosal",
            "notifications" : 4,
            "color" : "#9c27b0"
        , 
            "title" : "Operating System",
            "active" : true,
            "faculty" : "Niharika Singh",
            "notifications" : 0,
            "color" : "#ffc107"
        , 
            "title" : "Algorithms",
            "active" : true,
            "faculty" : "Debojit Mondal",
            "notifications" : 1,
            "color" : "#009688"
        , 
            "title" : "Web Technologies",
            "active" : true,
            "faculty" : "Shantanu Saurabh",
            "notifications" : 2,
            "color" : "#ff5722"
        , 
            "title" : "Formal Language and Automata Theory",
            "active" : true,
            "faculty" : "Sudhanshu Sekhar",
            "notifications" : 3,
            "color" : "#03a9f4"
        , 
            "title" : "Digital Logic and Circuit Design",
            "active" : false,
            "faculty" : "",
            "notifications" : 0,
            "color" : "#9e9e9e"
        
    ]

【讨论】:

您的解决方案有效。但是,我仍然不明白这是怎么回事:jsonplaceholder.typicode.com/users/1 是一个有效的 json 文件,而我创建的文件不是。它们很相似。 它是一个合法的json,但是如果你想使用json-server访问user.json的任何属性,该属性不能是数字或字符串,它应该是一个对象或数组对象。

以上是关于不支持 user.json 中的“id”(数字)类型。使用 json-server 时使用对象或对象数组出错,为啥?的主要内容,如果未能解决你的问题,请参考以下文章

$add 仅支持数字或日期类型,不支持数组在 Mongoose 中递增时出错

localhost/get/user.json localhost/get/user.xml

数字属性和类的 Java 决策树

ORA-01722:无效数字

MySQL数据库基础知识002

使用python将json转换为csv