【问题标题】:Return JSON Objects instead of JSON arrays with JSON-SERVER使用 JSON-SERVER 返回 JSON 对象而不是 JSON 数组
【发布时间】:2016-03-01 11:26:35
【问题描述】:

我正在使用json-server 将 json 数据作为 REST api 推送。

服务器返回(上图)这样的获取请求:

http://localhost:3000/items?itemid=534

返回:

[
  {
    "itemid": 534,
    "type": "textcontent",
    "icon": "http://exampple.com/1png",
    "title": "John",
    "description": "Description",
    "url": "http://example.net/project/doSomething"
  }
]

返回是JSON array,但不是JSON object。

我应该怎么做,返回 json 对象

数据文件为:data.json

{
  "items": [
    {
      "itemid": 534,
    "type": "textcontent",
    "icon": "http://exampple.com/1png",
    "title": "John",
    "description": "Description",
    "url": "http://example.net/project/doSomething"
    },
    {
      "itemid": 234,
    "type": "textcontent",
    "icon": "http://exampple.com/2png",
    "title": "Smith",
    "description": "Description2",
    "url": "http://example.net/project/doSomething2"
    }
    ]
}

【问题讨论】:

  • 你是问在客户端收到 JSON 数组后如何使用 rxjs 将其转换为 JSON 对象吗?你能澄清一下吗?
  • 是的,一样。 RxJs,CycleJS
  • json-server 有故障

标签: arrays json rxjs cyclejs


【解决方案1】:

如果您将数据更改为这样:

{
  "items": [
    {
      "id": 534,
      "type": "textcontent",
      "icon": "http://exampple.com/1png",
      "title": "John",
      "description": "Description",
      "url": "http://example.net/project/doSomething"
    },
    {
      "id": 234,
      "type": "textcontent",
      "icon": "http://exampple.com/2png",
      "title": "Smith",
      "description": "Description2",
      "url": "http://example.net/project/doSomething2"
    }
  ]
}

然后您可以使用以下网址访问各个项目: http://localhost:3000/items/534

【讨论】:

  • 感谢 Sami,但问题不在于 queryString 参数。检索不是数组而是对象的问题
【解决方案2】:

这很简单。您需要做的就是将数据映射到客户端上。我使用了 Rx.Observable.from 方法,但这将适用于其他其他方法,例如 fromPromise。

const data = [{
    "itemid": 534,
    "type": "textcontent",
    "icon": "http://exampple.com/1png",
    "title": "John",
    "description": "Description",
    "url": "http://example.net/project/doSomething"
  }
];

const data$ = Rx.Observable.from(data)
  .map(arr=> {
     return JSON.stringify({
       "items": arr
     });
  });

 data$.subscribe(x=> console.log(x));
 // this will print out something like this
//{"items":{"itemid":534,"type":"textcontent","icon":"http://exampple.com/1png","title":"John","description":"Description","url":"http://example.net/project/doSomething"}}

另外请注意,如果您事后只需要一个对象,则可以只返回 {'items':arr}。

【讨论】:

    【解决方案3】:

    我认为这是您正在使用的 URL 方案的期望行为。如果您使用标识单个资源的 URL,服务器应该只返回一个对象。

    当您使用查询参数时,您的请求会显示“请返回 item_id 为 534 的所有匹配项目”。这意味着 json-server 当然会返回一个项目数组。没有特别的知识,这个itemid 是独一无二的。如果你想指定一个特殊的项目,你应该使用特定项目的 URL,它应该看起来像 http://localhost:3000/items/534。

    这篇文章很好地解释了这些概念:http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

    更新

    我把你的问题弄错了。您希望将所有结果数据包含在一个特殊键下,而不是直接包含在数组值下。查看json-server 的文档似乎很容易扩展现有行为。 router.render() 功能能否帮助您实现目标? https://github.com/typicode/json-server#module

    router.render = function (req, res) {
      res.jsonp({
        yourkey: res.locals.data
      })
    }
    

    如果您希望键与您查询的资源类型相匹配,您可能需要查看req 对象以找出请求的实体类型。

    【讨论】:

    • 谢谢@ajaegle,但我需要所有项目的响应不应该作为对象返回
    • 您的回答很有用,但我发现 .map(res => JSON.parse(res.text)) JSON.parse 的解决方案有助于解析此对象。如果您认为您的帖子必须标记为 aswer,我准备好了
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 2013-09-23
    • 2018-07-03
    • 2020-08-03
    • 1970-01-01
    • 2015-07-10
    • 2021-11-03
    • 2022-01-06
    相关资源
    最近更新 更多