且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否将原始类型视为JSON?

更新时间:2023-01-22 20:42:58

{ "astring" }是无效的JSON,"astring"astring都不有效,因为我们需要键和值,例如{ KEY : VALUE }其中KEY始终是字符串,而VALUE可以是字符串,数字,布尔值或null.

{ "astring" } is not valid JSON and neither is "astring" or astring, as we need both a key and a value, e.g. { KEY : VALUE } where KEY is always a string and VALUE can be a string, number, boolean, or null.

根据规格:

是的,正如规范所说,JSON可以是***原始值,而无需 包装它的对象. –安迪·雷

Yes, as the spec says, JSON can be a top level primitive value without an object wrapping it. – Andy Ray

如果我正确理解,则该评论不正确.有效的JSON本身从来都不是***原始值.如果您仍然感到困惑,这应该可以解决问题:

If I understood it correctly, that comment is not correct. Valid JSON is never a top-level primitive value by itself. If you're still confused, this should clear things up:

  1. JSON语法

  1. JSON Grammar

JSON文本是令牌序列.令牌集包括 六个结构字符,字符串,数字和三个文字 名称.

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

JSON文本是序列化的对象或数组.

A JSON text is a serialized object or array.

JSON-text = object / array

这些是六个结构特征:

begin-array = ws %x5B ws ; [ left square bracket

begin-object = ws %x7B ws ; { left curly bracket

end-array = ws %x5D ws ; ] right square bracket

end-object = ws %x7D ws ; } right curly bracket

name-separator = ws %x3A ws ; : colon

value-separator = ws %x2C ws ; , comma

六个空格中的任何一个之前或之后都可以使用无关紧要的空格 结构特征.

Insignificant whitespace is allowed before or after any of the six structural characters.