Javascript 这个简单的字符串被认为是有效的 JSON 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7487869/
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
Is this simple string considered valid JSON?
提问by Christian
I've seen so many complicated questions in SO whether or not some complicated structure is considered to be valid JSON.
我在 SO 中看到了很多复杂的问题,无论某些复杂的结构是否被认为是有效的 JSON。
But what about something on the other end of the spectrum?
但是光谱另一端的东西呢?
"12345"
Is the above valid JSON?
以上是有效的 JSON 吗?
回答by Facebook Staff are Complicit
Yes, in most contexts. It is valid JSON syntax representing a JSON value.
是的,在大多数情况下。它是表示 JSON 值的有效 JSON 语法。
The confusion around this comes from Douglas Crockford's RFC 4627, which originally defined the application/json
internet media type in 2006. It said that:
对此的困惑来自 Douglas Crockford 的RFC 4627,它最初application/json
在 2006 年定义了互联网媒体类型。它说:
A JSON text is a serialized object or array.
JSON 文本是序列化的对象或数组。
However, as Crockford explained in a post in 2013 (unfortunately deleted with rest of Google+):
然而,正如 Crockford 在 2013 年的一篇帖子中所解释的那样(不幸的是被 Google+ 的其余部分删除了):
JSON is just a grammar, and the grammar includes numbers and strings. Uses of JSON must necessarily be more restrictive. RFC-4627 is one possible use, and was never intended to be the standard for JSON itself.
JSON 只是一种语法,语法包括数字和字符串。JSON 的使用必须更加严格。RFC-4627 是一种可能的用途,并且从未打算成为 JSON 本身的标准。
The example string is a valid JSON value, but it would have been incorrect to use it as the full "JSON text" body of an application/json
HTTP response. However, that's no longer true: RFC-4627 was obsoleted in 2014 with the publication of RFC 7159, which lets you use any JSON value:
示例字符串是一个有效的 JSON 值,但将其用作application/json
HTTP 响应的完整“JSON 文本”正文是不正确的。然而,这不再是事实:RFC-4627 在 2014 年随着RFC 7159的发布而过时,它允许您使用任何 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 文本限制为对象或数组。
A "standard for JSON itself" was also published in 2013, as ECMA-404, and JSON was also defined in edition 5.1 of the ECMAScript (JavaScript) specification ECMA-262. These specifications and most parsers allow any JSON value as a complete JSON text, even if it's just a simple string.
“JSON 本身的标准”也在 2013 年发布,作为ECMA-404,并且 JSON 也在 ECMAScript (JavaScript) 规范ECMA-262的第 5.1 版中定义。这些规范和大多数解析器允许任何 JSON 值作为完整的 JSON 文本,即使它只是一个简单的字符串。
回答by Robin Green
As of 2014, RFC 7159 obsoletes the older JSON RFCs, and declares that anyJSON value is valid JSON text and valid application/json content - including strings. However, it also points out the incompatibility issue with older JSON implementations:
截至 2014 年,RFC 7159 废弃了旧的 JSON RFC,并声明任何JSON 值都是有效的 JSON 文本和有效的应用程序/json 内容 - 包括字符串。但是,它还指出了与旧 JSON 实现的不兼容问题:
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 文本。
回答by Daniel A. White
Its a valid JSON string, but its not a JSON object.
它是一个有效的 JSON 字符串,但它不是一个 JSON 对象。
回答by Quentin
It is a valid JSON string, but isn't a valid JSON text (i.e. JSON file).
它是有效的 JSON 字符串,但不是有效的 JSON 文本(即 JSON 文件)。
See the JSON specificationwhich says:
请参阅JSON 规范,其中说:
A JSON text is a serialized object or array.
JSON 文本是序列化的对象或数组。
So you can use that inside some JSON, but isn't a complete JSON file by itself.
所以你可以在一些 JSON 中使用它,但它本身并不是一个完整的 JSON 文件。
(It is also worth noting that the specification defines a JSON parser:
(还值得注意的是,规范定义了一个 JSON 解析器:
A JSON parser transforms a JSON textinto another representation.
JSON 解析器将JSON 文本转换为另一种表示形式。
my <em>
)
我的<em>
)
For a second opinion, see JSON Lint:
对于第二种意见,请参阅JSON Lint:
Parse error on line 1:
"12345"
^
Expecting '{', '['