json - JSON 可以以 "["开头吗?

根据我在 json.org 上看到的内容,所有 JSON 字符串都应以 {(大括号)开头,[ 个字符(方括号)表示 JSON 中的数组元素。

我使用 json4j 库,我得到一个以 [ 开头的输入,所以我认为这不是有效的 JSON。我简要地查看了 JSON 模式,但我真的找不到它指出 JSON 文件不能以 [ 开头,或者它只能以 { 开头。

最佳答案

JSON 可以是数组或对象。特别是 json.org:

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an
    array, vector, list, or sequence.

然后继续将这两种结构描述为:

注意开始和结束字符分别是大括号和方括号。

编辑
从这里:http://www.ietf.org/rfc/rfc4627.txt

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

A JSON text is a serialized object or array.

更新(2014 年)

截至 2014 年 3 月,有一个新的 JSON RFC (7159) 稍微修改了定义(参见第 4/5 页)。

根据 RFC 4627 的定义是:JSON-text = object/array

这已在 RFC 7159 中更改为:JSON-text = ws value ws

其中ws代表空格,value定义如下:

A JSON value MUST be an object, array, number, or string, or one of the following three literal names:

false null true

所以,这个问题的答案仍然是肯定的,JSON 文本可以以方括号开头(即数组)。但除了对象和数组之外,它现在还可以是数字、字符串或值 falsenulltrue

此外,这与我之前的 RFC 4627 引用有所不同(添加了重点):

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

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 可以以 "["开头吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5034444/

相关文章:

json - JSON可以容纳多少有限制吗?

json - 如何使用命令行计算 JSON 对象中的项目?

json - 如何将 JSON 字符串转换为字典?

javascript - 何时更喜欢 JSON 而不是 XML?

json - 从结构中删除字段或将它们隐藏在 JSON 响应中

javascript - 最低有效 JSON 是多少?

json - 如何使用 Go 漂亮地打印 JSON?

javascript - 多个参数与选项对象

asp.net-mvc - 如何使用 Razor 将未编码的 Json 写入我的 View ?

json - 将 Map 转换为 POJO