布尔值是否有效 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).
有效:
无效:
推荐答案
您链接到的验证器根据 RFC 4627,它规定 JSON 字符串的根是数组或对象:
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
true
或 foo"
或 42
等未包装值不是 JSON 根据该 RFC.
An unwrapped value such as true
or "foo"
or 42
is not JSON according to that RFC.
它可以验证的其他 RFC,RFC 7159 和RFC 8259,认为上述示例有效,因为它不限制JSON文本到对象或数组,也允许values:
The other RFCs it can validate against, RFC 7159 and RFC 8259, deem 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 分别为 8259)淘汰,true
是一个有效的 JSON 字符串.
And because the former (RFC 4627) is obsoleted by the latter two (RFC 7159 respectively 8259), true
is a valid JSON string.
这篇关于布尔值是否有效 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!