布尔值是有效的JSON吗 [英] Are booleans valid JSON
问题描述
为什么有些JSON验证器将布尔值标记为无效JSON,而另一些则将其标记为有效?也就是说,只有true
或false
值(无对象).
Why do some JSON validators flag a boolean value as invalid JSON, and others as valid? That is, just true
or false
values (no object).
有效:
- http://jsonlint.com/
- http://codebeautify.org/jsonvalidator
无效:
推荐答案
您链接到的验证器根据
The validator you link to validates the JSON string existing of a mere true
as invalid according to RFC 4627, which dictates that the root of a JSON string is to be an array or object:
JSON文本是序列化的对象或数组.
A JSON text is a serialized object or array.
JSON-text = object / array
根据该RFC ,未包装的值(例如true
或"foo"
或42
)不是JSON .
An unwrapped value such as true
or "foo"
or 42
is not JSON according to that RFC.
可以验证的其他RFC RFC 7159 认为以上示例有效,因为它不会将JSON文本限制为对象或数组,但还允许 values :
The other RFC it can validate against, RFC 7159, deems the above examples valid as it does not constrain a JSON text to objects or arrays, but also allows values:
JSON值必须是对象,数组,数字或字符串,或者是以下之一 以下三个文字名称:
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
false null true
由于前者(RFC 4627)已被后者(RFC 7159)淘汰,因此true
是有效的JSON字符串.
And because the former (RFC 4627) is obsoleted by the latter (RFC 7159), true
is a valid JSON string.
这篇关于布尔值是有效的JSON吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!