不支持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?

查看:45
本文介绍了不支持user.json中的"id"(数字)类型.使用json-server时使用对象或对象数组错误,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 4应用程序.我正在尝试从JSON文件获取数据以构建用户仪表板.

I am working on an Angular 4 App. I am trying to get data from a JSON file for building a user dashboard.

我创建了一个json文件,并尝试使用JSON服务器使用以下代码进行加载: $ json-server --watch user.json ,并且出现以下错误:

I created a json file and tried loading it using JSON server using this: $ json-server --watch user.json and I am getting the following error:

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

当我删除文件中的"id"字段时,出现与"name"相同的错误.

When I remove the "id" field in the file, I get the same error with "name".

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

I feel there is something wrong with the way I have created the json files. I am new to web development, so don't mind if it's a very fundamental mistake.

这是我的json文件:

This is my json file:

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

推荐答案

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

That error tells you what to do. Use objects or arrays of objects

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

Id should be object or arrays of objects, same for the rest of the properties.

也许您应该考虑编辑user.json

Maybe you should consider edit your 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"
        }
    ]
}

这篇关于不支持user.json中的"id"(数字)类型.使用json-server时使用对象或对象数组错误,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆