简体   繁体   English

如何从 json-server 托管的 API 访问数组中的对象

[英]How do I access an object in an array from an API hosted by json-server

I'm using json-server for hosting an API in my project.我正在使用 json-server 在我的项目中托管 API。

What json-server gives me is http://localhost:3000/records . json-server 给我的是http://localhost:3000/records

{
    "record": [
        {
            "userID": "2",
            "userFirstName": "Arya",
            "userLastName": "Stark",
            "userMiddleName": "null",
            "roleName": "Admin",
            "roleID": "1"
        },
        {
            "userID": "1",
            "userFirstName": "John",
            "userLastName": "Snow",
            "userMiddleName": "null",
            "roleName": "Moderator",
            "roleID": "1"
        }
    ]
}

I'm using Postman for making GET request to the server.我正在使用 Postman 向服务器发出GET请求。

How do I go into record array and access the object with userID as 2 ?如何进入record数组并访问userID2的对象?

I tried this way but it's returning an empty object.我试过这种方式,但它返回一个空对象。

Please correct me if I'm wrong:如果我错了,请纠正我:

http://localhost:3000/records/record?userID=2

My actual JSON object file:我的实际 JSON 对象文件:

{
  "records": {
    "record": [
      {
        "userID": "2",
        "userFirstName": "Arya",
        "userLastName": "Stark",
        "userMiddleName": "null",
        "roleName": "Admin",
        "roleID": "1"
      },
      {
        "userID": "1",
        "userFirstName": "John",
        "userLastName": "Snow",
        "userMiddleName": "null",
        "roleName": "Moderator",
        "roleID": "1"
      }
    ]
  }
}

Just so slightly change your JSON structure:稍微改变一下你的 JSON 结构:

{
  "records": [
    {
      "userID": "2",
      "userFirstName": "Arya",
      "userLastName": "Stark",
      "userMiddleName": "null",
      "roleName": "Admin",
      "roleID": "1"
    },
    {
      "userID": "1",
      "userFirstName": "John",
      "userLastName": "Snow",
      "userMiddleName": "null",
      "roleName": "Moderator",
      "roleID": "1"
    }
  ]
}

Then http://localhost:3000/records?userID=2 returns然后http://localhost:3000/records?userID=2返回

[
  {
    "userID": "2",
    "userFirstName": "Arya",
    "userLastName": "Stark",
    "userMiddleName": "null",
    "roleName": "Admin",
    "roleID": "1"
  }
]

Aside from this solving the actual access problem I was also unable to find a solution to filter based on object properties that are part of an array which is stored in a property of your db.json object.除了解决实际访问问题之外,我还无法找到基于对象属性进行过滤的解决方案,这些对象属性是存储在 db.json 对象的属性中的数组的一部分。

Simple but have you tried just using /records/record/2 the json-server module takes most of the implementation away from the user and this may just work, as it's designed to be a quick REST API to get people started.很简单,但您是否尝试过只使用/records/record/2 json-server模块从用户那里拿走了大部分实现,这可能会起作用,因为它被设计为一个快速的 REST API 来让人们开始。

Check out the examples on the documentation page or take a look at the API running here http://jsonplaceholder.typicode.com/ as it's using the same code.查看文档页面上的示例或查看此处运行的 API http://jsonplaceholder.typicode.com/因为它使用相同的代码。

You have to change something on your server side to filter request according to the request parameters您必须在服务器端更改某些内容以根据请求参数过滤请求

Or just use javascript:或者只是使用javascript:

data.records.record.filter((user)=>user.userID==2)[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM