这个简单的字符串被认为是有效的 JSON 吗? [英] Is this simple string considered valid JSON?

查看:24
本文介绍了这个简单的字符串被认为是有效的 JSON 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SO 中看到了很多复杂的问题,无论某些复杂的结构是否被认为是有效的 JSON.

I've seen so many complicated questions in SO whether or not some complicated structure is considered to be valid JSON.

但是光谱的另一端呢?

"12345"

以上是有效的 JSON 吗?

Is the above valid JSON?

推荐答案

是的,在大多数情况下.它是表示 JSON 值的有效 JSON 语法.

Yes, in most contexts. It is valid JSON syntax representing a JSON value.

对此的混淆来自 Douglas Crockford 的 RFC 4627,它最初定义了application/json 2006 年的网络媒体类型.它说:

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:

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

A JSON text is a serialized object or array.

然而,正如 Crockford 在 2013 年的一篇帖子中所解释的那样(不幸的是被 Google+ 的其余部分删除,但 存档于此处):

However, as Crockford explained in a post in 2013 (unfortunately deleted with rest of Google+, but archived here):

JSON 只是一种语法,语法包括数字和字符串.JSON 的使用必须更加严格.RFC-4627 是一种可能的用途,但从未打算成为 JSON 本身的标准.

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 文本"是不正确的.application/json HTTP 响应的主体.然而,事实不再如此:RFC-4627 在 2014 年随着 RFC 7159,它允许您使用任何 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 文本是一个序列化值.请注意,某些先前的 JSON 规范将 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 本身的标准";也在 2013 年发布,作为 ECMA-404 和 JSONECMAScript (JavaScript) 规范 5.1 版中也定义了 ECMA-262.这些规范和大多数解析器允许任何 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 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆