什么是最小有效JSON? [英] What is the minimum valid JSON?

查看:109
本文介绍了什么是最小有效JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仔细阅读了 http://json.org/ 的JSON说明,但我不确定我是否知道答案简单的问题。哪些字符串是最小可能的有效JSON?

I've carefully read the JSON description http://json.org/ but I'm not sure I know the answer to the simple question. What strings are the minimum possible valid JSON?


  • string是字符串有效的JSON?

  • 42 是简单的有效JSON号码?

  • true 是有效JSON的布尔值吗?

  • {} 是空对象有效JSON?

  • [] 空数组是有效的JSON吗?

  • "string" is the string valid JSON?
  • 42 is the simple number valid JSON?
  • true is the boolean value a valid JSON?
  • {} is the empty object a valid JSON?
  • [] is the empty array a valid JSON?

推荐答案

在撰写本文时,JSON仅在 RFC4627 。它将JSON文本(在2的开头)描述为序列化对象或数组。

At the time of writing, JSON was solely described in RFC4627. It describes (at the start of "2") a JSON text as being a serialized object or array.

这意味着 {} [] 是符合该标准的解析器和字符串中有效的完整JSON字符串。

This means that only {} and [] are valid, complete JSON strings in parsers and stringifiers which adhere to that standard.

然而,ECMA-404的引入改变了这一点,并且更新了建议可以在这里阅读。我也写了博文关于这个问题。

However, the introduction of ECMA-404 changes that, and the updated advice can be read here. I've also written a blog post on the issue.

然而,为了进一步混淆这个问题, JSON web中可用的对象(例如 JSON.parse() JSON.stringify())浏览器在ES5中标准化,并明确定义了可接受的JSON文本,如下所示:

To confuse the matter further however, the JSON object (e.g. JSON.parse() and JSON.stringify()) available in web browsers is standardised in ES5, and that clearly defines the acceptable JSON texts like so:


本规范中使用的JSON交换格式正是RFC 4627所描述的格式,但有两个例外:

The JSON interchange format used in this specification is exactly that described by RFC 4627 with two exceptions:


  • ECMAScript JSON语法的顶级JSONText生成可能包含任何JSONValue,而不是限制为RFC 4627指定的JSONObject或JSONArray。

  • The top level JSONText production of the ECMAScript JSON grammar may consist of any JSONValue rather than being restricted to being a JSONObject or a JSONArray as specified by RFC 4627.

snipped

这意味着所有 JSON va即使JSON对象在技术上遵守RFC 4627,JSON对象也接受lues(包括字符串,空值和数字)。

This would mean that all JSON values (including strings, nulls and numbers) are accepted by the JSON object, even though the JSON object technically adheres to RFC 4627.

请注意,您可以对字符串进行字符串化在符合浏览器的情况下,通过 JSON.stringify(5),这将被另一个符合RFC4627的解析器拒绝,但没有上面列出的特定异常。例如,Ruby,似乎就是这样的接受对象和数组作为根的示例。另一方面,PHP,特别添加了它还将编码和解码标量类型和NULL的异常

Note that you could therefore stringify a number in a conformant browser via JSON.stringify(5), which would be rejected by another parser that adheres to RFC4627, but which doesn't have the specific exception listed above. Ruby, for example, would seem to be one such example which only accepts objects and arrays as the root. PHP, on the other hand, specifically adds the exception that "it will also encode and decode scalar types and NULL".

这篇关于什么是最小有效JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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