一个字符串值是否被认为是有效的JSON?(复制)

时间:2022-10-28 16:06:48

This question already has an answer here:

这个问题已经有了答案:

Do you consider the JSON web response:

您是否考虑JSON web响应:

"A serialization error occurred"

“一个序列化错误发生”

to be valid? Or not

是有效的吗?或不

Some validator accepts it : http://www.freeformatter.com/json-validator.html

一些验证器接受它:http://www.freeformatter.com/json-validator.html

And some other not.

和其他一些不是。

Sylvain

Sylvain

5 个解决方案

#1


24  

As for new JSON RFC, json, containing only single value is pretty valid.

对于新的JSON RFC,只包含单个值的JSON是非常有效的。

A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array.

JSON文本是一个序列化的值。注意,JSON的某些先前规范将JSON文本约束为对象或数组。

#2


16  

There's a change of heart on this between RFC4627 and RFC7159:

在RFC4627和RFC7159之间,人们的看法发生了变化:

RFC4627:

RFC4627:

A JSON text is a serialized object or array.

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

  JSON-text = object / array

RFC7159:

RFC7159:

A JSON text is a serialized value. Note that certain previous
specifications of JSON constrained a JSON text to be an object or an
array. Implementations that generate only objects or arrays where a
JSON text is called for will be interoperable in the sense that all
implementations will accept these as conforming JSON texts.

JSON文本是一个序列化的值。请注意,某些先前的JSON规范限制了JSON文本为对象或数组。只生成调用JSON文本的对象或数组的实现将是可互操作的,因为所有实现都将接受它们作为符合JSON文本的。

  JSON-text = ws value ws

No philosophical or practical justification is provided for this change of heart. The earlier version probably makes more sense as it consistently dictates that both a singe list element and a single map element (a pair or tuple) be contained. The second version allows only a single list element to be uncontained.

没有哲学或实际的理由为这种改变的心提供。早期的版本可能更有意义,因为它始终规定要包含单个列表元素和单个映射元素(一对或元组)。第二个版本只允许不包含一个列表元素。

#3


10  

According to the grammar exposed in http://www.json.org/ (which references the Standard ECMA-262 3rd Edition - December 1999 par.5.1.5 The JSON Grammar) it's wrong:

根据http://www.json.org/(引用标准的ECMA-262第三版- 1999年12月第5.1.5段JSON语法)中的语法,它是错误的:

The initial element must be:

初始元素必须是:

一个字符串值是否被认为是有效的JSON?(复制)

一个字符串值是否被认为是有效的JSON?(复制)

and then a value can be a string:

然后一个值可以是一个字符串:

一个字符串值是否被认为是有效的JSON?(复制)

#4


0  

From RFC4627:

从RFC4627:

A JSON text is a serialized object or array.
    JSON-text = object / array

IE, the root element has to be an object or array, and can't be a string value by itself.

例如,根元素必须是一个对象或数组,它本身不能是一个字符串值。

#5


-3  

I don't care if some validator accepts it. It's wrong. It's a question of good practice, Json format must be {"key": "value", .....}. If you consider that text Json, can work, but for the rest of programmer it's not a serious Json. If you use only that text, then you don't need Json.

我不关心是否有验证者接受它。这是错误的。这是一个好的实践问题,Json格式必须是{“关键”:“价值”,…}。如果您认为这个文本Json可以工作,但是对于其他程序员来说,它并不是一个真正的Json。如果只使用该文本,则不需要Json。

#1


24  

As for new JSON RFC, json, containing only single value is pretty valid.

对于新的JSON RFC,只包含单个值的JSON是非常有效的。

A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array.

JSON文本是一个序列化的值。注意,JSON的某些先前规范将JSON文本约束为对象或数组。

#2


16  

There's a change of heart on this between RFC4627 and RFC7159:

在RFC4627和RFC7159之间,人们的看法发生了变化:

RFC4627:

RFC4627:

A JSON text is a serialized object or array.

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

  JSON-text = object / array

RFC7159:

RFC7159:

A JSON text is a serialized value. Note that certain previous
specifications of JSON constrained a JSON text to be an object or an
array. Implementations that generate only objects or arrays where a
JSON text is called for will be interoperable in the sense that all
implementations will accept these as conforming JSON texts.

JSON文本是一个序列化的值。请注意,某些先前的JSON规范限制了JSON文本为对象或数组。只生成调用JSON文本的对象或数组的实现将是可互操作的,因为所有实现都将接受它们作为符合JSON文本的。

  JSON-text = ws value ws

No philosophical or practical justification is provided for this change of heart. The earlier version probably makes more sense as it consistently dictates that both a singe list element and a single map element (a pair or tuple) be contained. The second version allows only a single list element to be uncontained.

没有哲学或实际的理由为这种改变的心提供。早期的版本可能更有意义,因为它始终规定要包含单个列表元素和单个映射元素(一对或元组)。第二个版本只允许不包含一个列表元素。

#3


10  

According to the grammar exposed in http://www.json.org/ (which references the Standard ECMA-262 3rd Edition - December 1999 par.5.1.5 The JSON Grammar) it's wrong:

根据http://www.json.org/(引用标准的ECMA-262第三版- 1999年12月第5.1.5段JSON语法)中的语法,它是错误的:

The initial element must be:

初始元素必须是:

一个字符串值是否被认为是有效的JSON?(复制)

一个字符串值是否被认为是有效的JSON?(复制)

and then a value can be a string:

然后一个值可以是一个字符串:

一个字符串值是否被认为是有效的JSON?(复制)

#4


0  

From RFC4627:

从RFC4627:

A JSON text is a serialized object or array.
    JSON-text = object / array

IE, the root element has to be an object or array, and can't be a string value by itself.

例如,根元素必须是一个对象或数组,它本身不能是一个字符串值。

#5


-3  

I don't care if some validator accepts it. It's wrong. It's a question of good practice, Json format must be {"key": "value", .....}. If you consider that text Json, can work, but for the rest of programmer it's not a serious Json. If you use only that text, then you don't need Json.

我不关心是否有验证者接受它。这是错误的。这是一个好的实践问题,Json格式必须是{“关键”:“价值”,…}。如果您认为这个文本Json可以工作,但是对于其他程序员来说,它并不是一个真正的Json。如果只使用该文本,则不需要Json。