JSON可以以"["开头吗? [英] Can JSON start with "["?

查看:405
本文介绍了JSON可以以"["开头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

From what I can read on json.org, all JSON strings should start with { (curly brace), and [ characters (square brackets) represent an array element in JSON.

我使用json4j库,并且得到了以[开头的输入,因此我认为这不是有效的JSON.我简要地看了一下JSON模式,但是我找不到它表明JSON文件不能以[开头,或者只能以{开头.

I use the json4j library, and I got an input that starts with [, so I didn't think this was valid JSON. I looked briefly at the JSON schema, but I couldn't really find it stated that a JSON file cannot start with [, or that it can only start with {.

推荐答案

JSON可以是数组或对象.专门针对json.org:

JSON can be either an array or an object. Specifically off of json.org:

JSON建立在两个结构上:

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.

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

It then goes on to describe the two structures as:

请注意,起始字符和结束字符分别是大括号和方括号.

Note that the starting and ending characters are curly brackets and square brackets respectively.

修改
从这里开始: http://www.ietf.org/rfc/rfc4627.txt

Edit
And from here: http://www.ietf.org/rfc/rfc4627.txt

JSON文本是令牌序列. 令牌集包括六个 结构字符,字符串, 数字和三个文字名称.

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

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

A JSON text is a serialized object or array.

更新(2014)

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

Update (2014)

As of March 2014, there is a new JSON RFC (7159) that modifies the definition slightly (see pages 4/5).

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

The definition per RFC 4627 was: JSON-text = object / array

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

This has been changed in RFC 7159 to: JSON-text = ws value ws

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

Where ws represents whitespace and value is defined as follows:

JSON值必须是对象,数组,数字或字符串,或者是以下之一 以下三个文字名称:

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

false null true

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

So, the answer to the question is still yes, JSON text can start with a square bracket (i.e. an array). But in addition to objects and arrays, it can now also be a number, string or the values false, null or true.

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

Also, this has changed from my previous RFC 4627 quote (emphasis added):

JSON文本是令牌序列.令牌集包括六个 结构字符,字符串,数字和三个文字名称.

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

JSON文本是序列化的.请注意,某些先前的 JSON规范将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. 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可以以"["开头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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