JSON可以以“["开头吗? [英] Can JSON start with "["?
问题描述
根据我在
<块引用>JSON 文本是一个标记序列.该组令牌包括六个结构字符、字符串、数字和三个字面名称.
JSON 文本是一个序列化的对象或数组.
更新(2014 年)
截至 2014 年 3 月,有一个新的 JSON RFC (7159)稍微修改了定义(参见第 4/5 页).
根据 RFC 4627 的定义是:JSON-text = object/array
这已在 RFC 7159 中更改为:JSON-text = ws value ws
其中ws
代表空格,value
定义如下:
JSON 值必须是对象、数组、数字或字符串,或其中之一以下三个字面名称:
false null true
所以,这个问题的答案仍然是肯定的,JSON 文本可以以方括号开头(即数组).但除了对象和数组之外,它现在还可以是数字、字符串或值 false
、null
或 true
.
此外,这与我之前的 RFC 4627 引用有所不同(添加了重点):
<块引用>JSON 文本是一个标记序列.该组令牌包括六个结构字符、字符串、数字和三个文字名称.
JSON 文本是一个序列化的值.请注意,某些先前的JSON 规范将 JSON 文本限制为对象或大批.仅生成对象或数组的实现要求 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.
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 can be either an array or an object. Specifically off of 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.
It then goes on to describe the two structures as:
Note that the starting and ending characters are curly brackets and square brackets respectively.
Edit
And from here: 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.
Update (2014)
As of March 2014, there is a new JSON RFC (7159) that modifies the definition slightly (see pages 4/5).
The definition per RFC 4627 was: JSON-text = object / array
This has been changed in RFC 7159 to: JSON-text = ws value ws
Where ws
represents whitespace and value
is defined as follows:
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
false null true
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
.
Also, this has changed from my previous RFC 4627 quote (emphasis added):
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可以以“["开头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!