单个字符串值是否被视为有效的 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13318420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:37:02  来源:igfitidea点击:

Is a single string value considered valid JSON?

json

提问by Sylvain Demers

Do you consider the JSON web response:

您是否考虑过 JSON 网络响应:

"A serialization error occurred"

to be valid or not?

是否有效?

Some validators accept itwhile others don't.

一些验证器接受它,而另一些则不接受

回答by igor

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 文本限制为对象或数组。

回答by Basel Shishani

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

RFC4627RFC7159之间有一个改变:

RFC4627:

RFC4627

A JSON text is a serialized object or array.

  JSON-text = object / 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-text = ws value ws

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.

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

回答by Tony Rad

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

根据http://www.json.org/ 中公开的语法(引用标准 ECMA-262 3rd Edition - December 1999par.5.1.5 The JSON Grammar),这是错误的:

The initial element must be:

初始元素必须是:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

and then a value can be a string:

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

enter image description here

在此处输入图片说明

回答by fgb

From RFC4627:

来自RFC4627

A JSON text is a serialized object or array.
    JSON-text = object / array
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.

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

回答by itaka

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 格式必须是{"key": "value", .....}. 如果您认为该文本 Json 可以工作,但对于其他程序员来说,这不是一个严重的 Json。如果您只使用该文本,则不需要 Json。