JSON值为1或0 - int或boolean

时间:2022-12-05 10:00:29

Does JSON treat these all the same? Or are they a mix of Integers and booleans?

JSON处理这些都一样吗?或者它们是整数和布尔值的组合?

var data =
{
    "zero" : 0,
    "one" : 1,
    "false" : 0,
    "true" : 1,
    "0" : false,
    "1" : true
}

4 个解决方案

#1


62  

JSON is a format for transferring data.
It has no notion of equality.

JSON是一种传输数据的格式。它没有平等的概念。

JSON parsers treat booleans and numbers as distinct types.

JSON解析器将布尔值和数字视为不同的类型。

#2


83  

The values true and false are actual boolean values, the rest are integers. See http://json.org/ for more.

真值和假值是实际的布尔值,其余是整数。见http://json.org/。

#3


23  

I prefer using 0/1 instead of true/false, because 0/1 consume only 1 byte while true/false consume 4/5 bytes.

我更喜欢使用0/1而不是true/false,因为0/1只消耗1字节,而true/false只消耗4/5字节。

#4


7  

As mentioned, at JSON level, 0 and false are not the same; data types are number versus boolean. But JSON processing libraries can choose to do conversions; especially on languages/platforms that do not have native boolean type, for example. In that case, another representation may be used (empty string or 0 for false).

如前所述,在JSON级别,0和false是不相同的;数据类型是数字还是布尔。但是JSON处理库可以选择进行转换;特别是在没有原生布尔类型的语言/平台上。在这种情况下,可以使用另一种表示(空字符串或0表示false)。

Further, it is also possible that processing libraries can coerce types: such that if a boolean value is expected, certain number/string values (or JSON 'null' token) can be accepted instead. This is fairly common, due to differences on data type choices on different languages.

此外,处理库还可以强制类型:如果期望布尔值,则可以接受某些数字/字符串值(或JSON 'null'令牌)。这很常见,因为不同语言的数据类型选择不同。

#1


62  

JSON is a format for transferring data.
It has no notion of equality.

JSON是一种传输数据的格式。它没有平等的概念。

JSON parsers treat booleans and numbers as distinct types.

JSON解析器将布尔值和数字视为不同的类型。

#2


83  

The values true and false are actual boolean values, the rest are integers. See http://json.org/ for more.

真值和假值是实际的布尔值,其余是整数。见http://json.org/。

#3


23  

I prefer using 0/1 instead of true/false, because 0/1 consume only 1 byte while true/false consume 4/5 bytes.

我更喜欢使用0/1而不是true/false,因为0/1只消耗1字节,而true/false只消耗4/5字节。

#4


7  

As mentioned, at JSON level, 0 and false are not the same; data types are number versus boolean. But JSON processing libraries can choose to do conversions; especially on languages/platforms that do not have native boolean type, for example. In that case, another representation may be used (empty string or 0 for false).

如前所述,在JSON级别,0和false是不相同的;数据类型是数字还是布尔。但是JSON处理库可以选择进行转换;特别是在没有原生布尔类型的语言/平台上。在这种情况下,可以使用另一种表示(空字符串或0表示false)。

Further, it is also possible that processing libraries can coerce types: such that if a boolean value is expected, certain number/string values (or JSON 'null' token) can be accepted instead. This is fairly common, due to differences on data type choices on different languages.

此外,处理库还可以强制类型:如果期望布尔值,则可以接受某些数字/字符串值(或JSON 'null'令牌)。这很常见,因为不同语言的数据类型选择不同。