在 JSON 中将缺失值显示为 null 或根本不显示
我正在构建一个 Web 服务 API,使用 JSON 作为数据语言。设计从服务返回的数据的结构时,我在决定如何处理缺失值时遇到了一些困难。
考虑这个例子:我的网上商店中有一种产品,其价格尚不清楚,可能是因为该产品尚未发布。我是否包含 price: null
(如下所示),还是直接忽略该商品的 price
属性?
{
name: 'OSX 10.6.10',
brand: 'Apple',
price: null
}
我主要关心的是使 API 尽可能易于使用。显式的 null 值清楚地表明产品可以预期价格,但另一方面它似乎浪费了字节。可能有一大堆与该特定产品完全无关的属性,但与其他产品相关 - 我是否也应该将这些属性显式显示为 null
?
{
name: 'OSX 10.6.10',
price: 29.95,
color: null,
size: null
}
Web 服务设计是否有支持显式或隐式空值的“最佳实践”?有事实上的标准吗?还是完全取决于用例?
I am building a web service API, using JSON as the data language. Designing the structure of the data returned from the service, I am having some trouble deciding how to deal with missing values.
Consider this example: I have a product in my web store for which the price is yet unknown, maybe because the product has not yet been released. Do I include price: null
(as shown below) or do I simply omit the price
property on this item?
{
name: 'OSX 10.6.10',
brand: 'Apple',
price: null
}
My main concern is making the API as easy to consume as possible. The explicit null value makes it clear that a price
can be expected on a product, but at the other hand it seems like wasted bytes. There could be a whole bunch of properties that are completely irrelevant to this particular product, while relevant for other products – should I show these as explicitly null
as well?
{
name: 'OSX 10.6.10',
price: 29.95,
color: null,
size: null
}
Are there any "best practices" on web service design, favoring explicit or implicit null values? Any de-facto standard? Or does it depend entirely on the use case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FWIW,我个人的看法:
我会将“标准”字段的值设置为
null
。尽管 JSON 经常与 JavaScript 一起使用,但缺失的属性可以像设置为 null 的属性一样进行处理,但对于其他语言(例如 Java)则不能是这种情况。必须首先测试某个字段是否存在似乎很不方便。将值设置为null
但保留字段会更加一致。我只会包含与产品相关的字段(例如,不包含 CD 的
pages
)。正确处理这些“可选”字段是客户的任务。如果与产品相关的某个字段没有值,也可以将其设置为null
。正如已经说过的,最重要的是保持一致并明确指定哪些字段是可以预期的。您可以使用 gzip 压缩来减小数据大小。
FWIW, my personal opinion:
I would set the values of "standard" fields to
null
. Although JSON is often used with JavaScript and there, missing properties can be handled similarly as the ones set to null, this must not be the case for other languages (e.g. Java). Having to test first whether a field is present seems inconvenient. Setting the values tonull
but having the fields present would be more consistent.I would only include those fields that are relevant for a product (e.g. not
pages
for a CD). It's the client's task to deal with these "optional" fields properly. If you have no value for a certain field which is relavant to a product, set it tonull
too.As already said, the most important thing is to be consistent and to clearly specify which fields can be expected. You can reduce the data size using gzip compression.
我不知道哪个是“最佳实践”。但我通常不会发送我不需要的字段。
当我读取响应时,我检查值是否存在:
i don't know which is "best practice". But i usually don't send fields that i don't need.
When i read response, i check if value exists: