如果空值被包含在从REST API JSON响应? [英] Should null values be included in JSON responses from a REST API?

查看:133
本文介绍了如果空值被包含在从REST API JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计和开发一个RESTful API的过程。我采取了务实,资源化的方式来对API(面向资源,统一的接口,寻址,但没有真正的HATEOAS)。有一点我不知道,虽然是如何在对象接近零值。

我应该包括空值的字段在API的反应?

例如:

  {
    FIELDA:AAA,
    fieldB:空
}

或者,我应该离开了这些领域完全如果系统有这些字段没有数据?

例如:

  {
    FIELDA:AAA
}


解决方案

有一个关于这个的讨论在最近的 API-工艺。普遍的共识是有可能是一个值的遗漏的之间的语义差别,相对于一个包含空值的。

如果有要得到你的具体使用情况下没有任何语义的价值,那么我会在你的目标消费者API的说一下,想想是否遗漏值将导致他们的问题。

I'm in the process of designing and developing a RESTful API. I'm taking a pragmatic, resource oriented approach to the API (resource oriented, uniform interface, addressability, but no real HATEOAS). One point I'm not sure about though is how to approach null values in objects.

Should I include fields with null values in the APIs responses?

Example:

{
    "fieldA": "AAA",
    "fieldB": null
}

Or, should I just leave out these fields altogether if the system has no data for these fields?

Example:

{
    "fieldA": "AAA"
}

解决方案

There was a discussion about this recently on API-Craft. The general consensus was there is potentially a semantic difference between the omission of a value, versus an inclusion of a null value.

If there is no semantic value to be gained for your specific use case, then I would say look at your target consumers of the API, and think about whether omitting the value will cause them problems.

这篇关于如果空值被包含在从REST API JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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