在JSON中,空和空的约定是什么?

时间:2021-03-20 11:11:28

I know that in most programming scenarios, the preference is for empty collections to null collections when there are 0 elements. However, most languages that consume JSON (like JavaScript) will treat empty lists/objects as true and null ones as false. For example this would be both true and an object in JavaScript:

我知道在大多数编程场景中,当有0个元素时,首选的是空集合到空集合。然而,大多数使用JSON的语言(如JavaScript)会将空列表/对象视为真,空列表/对象为假。例如,这是对的,也是JavaScript中的对象:

{
    "items_in_stock": {"widgets":10, "gadgets": 5}
}

But this is also true:

但这也是事实:

{
    "items_in_stock": {}
}

And this is false:

这是错误的:

{
    "items_in_stock": null
}

Is there a convention on empty objects/lists for JSON? And what about for numbers, booleans, and strings?

JSON的空对象/列表是否有约定?那么对于数字、布尔值和字符串呢?

2 个解决方案

#1


48  

It is good programming practice to return an empty array if the expected return type is an array. This makes sure that the receiver of the json can treat the value as an array immediately and does not first has to check if it is null. It's the same way with empty objects ({}). Strings, Booleans and integers do not have an 'empty' form, so there it is okay to use null values.

如果预期的返回类型是数组,那么返回空数组是很好的编程实践。这确保json的接收者可以立即将该值作为数组来处理,并且不需要首先检查它是否为null。空对象({})也是如此。字符串、布尔值和整数没有“空”形式,因此可以使用空值。

Don't take my word for it. In Joshua Blochs excellent book "Effective Java" he describes some very good generic programming practices (often applicable to other programming langages as well). Returning empty collections instead of nulls is one of them. Here's a link to that part of his book: http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html

别相信我的话。在Joshua Blochs的优秀著作《有效的Java》中,他描述了一些非常好的通用编程实践(通常也适用于其他编程语言)。返回空集合而不是null是其中之一。以下是他书中这一部分的链接:http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html

#2


4  

"JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types."

“JSON有一个特殊的值,称为null,它可以设置在任何类型的数据上,包括数组、对象、数字和布尔类型。”

"The JSON empty concept applies for arrays and objects...Data object does not have a concept of empty lists. Hence, no action is taken on the data object for those properties."

“JSON空的概念适用于数组和对象……”数据对象没有空列表的概念。因此,不会对这些属性的数据对象采取任何操作。

Here is my source.

这是我的源。

#1


48  

It is good programming practice to return an empty array if the expected return type is an array. This makes sure that the receiver of the json can treat the value as an array immediately and does not first has to check if it is null. It's the same way with empty objects ({}). Strings, Booleans and integers do not have an 'empty' form, so there it is okay to use null values.

如果预期的返回类型是数组,那么返回空数组是很好的编程实践。这确保json的接收者可以立即将该值作为数组来处理,并且不需要首先检查它是否为null。空对象({})也是如此。字符串、布尔值和整数没有“空”形式,因此可以使用空值。

Don't take my word for it. In Joshua Blochs excellent book "Effective Java" he describes some very good generic programming practices (often applicable to other programming langages as well). Returning empty collections instead of nulls is one of them. Here's a link to that part of his book: http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html

别相信我的话。在Joshua Blochs的优秀著作《有效的Java》中,他描述了一些非常好的通用编程实践(通常也适用于其他编程语言)。返回空集合而不是null是其中之一。以下是他书中这一部分的链接:http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html

#2


4  

"JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types."

“JSON有一个特殊的值,称为null,它可以设置在任何类型的数据上,包括数组、对象、数字和布尔类型。”

"The JSON empty concept applies for arrays and objects...Data object does not have a concept of empty lists. Hence, no action is taken on the data object for those properties."

“JSON空的概念适用于数组和对象……”数据对象没有空列表的概念。因此,不会对这些属性的数据对象采取任何操作。

Here is my source.

这是我的源。