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

时间:2017-09-13 10:51:38

标签: angularjs json json-server

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

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

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

当我删除" id"在文件中的字段中,我得到了与#34; name"。

相同的错误

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

这是我的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"
    }
  ]
}

2 个答案:

答案 0 :(得分:1)

也许我迟到的回应,但这只是为了未来的读者,因为我也面临类似的问题,直到我更多地了解json-server。

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

直接在JSON对象根目录中公开的每个键都被视为json-server 中的单独URL。在您的情况下,如果您将整个JSON对象包装在一个键中,例如:'数据',您的错误将得到解决。你的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如何工作的示例

答案 1 :(得分:0)

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

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"
        }
    ]
}