JSON 可以以“[”开头吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5034444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:45:59  来源:igfitidea点击:

Can JSON start with "["?

json

提问by Tiberiu

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.

从我在json.org上可以读到的,所有 JSON 字符串都应该以{(花括号)开头,[字符(方括号)代表 JSON 中的数组元素。

I use the json4jlibrary, 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 {.

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

回答by Richard Marskell - Drackir

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

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.

JSON 建立在两种结构上:

  • 名称/值对的集合。在各种语言中,这被实现为对象、记录、结构、字典、哈希表、键控列表或关联数组。
  • 值的有序列表。在大多数语言中,这被实现为
    数组、向量、列表或序列。

It then goes on to describe the two structures as: A JSON objectA JSON array

然后继续将这两个结构描述为: 一个 JSON 对象一个 JSON 数组

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

编辑
并从这里: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.

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

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

Update (2014)

更新 (2014)

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

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

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

每个 RFC 4627 的定义是: JSON-text = object / array

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

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

Where wsrepresents whitespace and valueis defined as follows:

其中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 值必须是对象、数组、数字或字符串,或以下三个字面名称之一:

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, nullor true.

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

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

此外,这与我之前的 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 文本是一个序列化的。请注意,某些先前的 JSON 规范将 JSON 文本限制为对象或数组。仅生成需要 JSON 文本的对象或数组的实现将是可互操作的,因为所有实现都将接受这些作为一致的 JSON 文本。

回答by Nathaniel Mills

If the string you are parsing begins with a left brace ([) you can use JSONArray.parseto get back a JSONArray object and then you can use get(i)where iis an index from 0 through the returned JSONArray's size()-1.

如果您正在解析的字符串以左括号 ([) 开头,您可以使用它JSONArray.parse来获取 JSONArray 对象,然后您可以使用get(i)其中i是从 0 到返回的 JSONArray 的索引size()-1

import java.io.IOException;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;

public class BookListTest {
   public static void main(String[] args) {
      String jsonBookList = "{\"book_list\":{\"book\":[{\"title\":\"title 1\"},{\"title\":\"title 2\"}]}}";
      Object book_list;
      try {
         book_list = JSONObject.parse(jsonBookList);
         System.out.println(book_list);
         Object bookList = JSONObject.parse(book_list.toString()).get("book_list");
         System.out.println(bookList);
         Object books = JSONObject.parse(bookList.toString()).get("book");
         System.out.println(books);
         JSONArray bookArray = JSONArray.parse(books.toString());
         for (Object book : bookArray) {
            System.out.println(book);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Which produced output like:

产生的输出如下:

{"book_list":{"book":[{"title":"title 1"},{"title":"title 2"}]}}
{"book":[{"title":"title 1"},{"title":"title 2"}]}
[{"title":"title 1"}, {"title":"title 2"}]
{"title":"title 1"}
{"title":"title 2"}

Note: if you attempted to call JSONObject.parse(books.toString());you would get the error you encountered:

注意:如果您尝试调用,JSONObject.parse(books.toString());您会遇到遇到的错误:

java.io.IOException: Expecting '{' on line 1, column 2 instead, obtained token: 'Token: ['

回答by J. Moreno

JSON.ORG WEBSITE SAYS ....

JSON.ORG 网站说....

https://www.json.org/

https://www.json.org/

The site clearly states the following:

该网站明确声明如下:

JSON is built on two structures:

JSON 建立在两种结构上:

  1. 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.

  2. An ordered list of values. In most languages, this is realized as an?array, vector, list, or sequence.

  1. 名称/值对的集合。在各种语言中,这被实现为对象、记录、结构、字典、哈希表、键控列表或关联数组。

  2. 值的有序列表。在大多数语言中,这被实现为数组、向量、列表或序列。

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures. In JSON, they take on these forms:

这些是通用数据结构。几乎所有现代编程语言都以一种或另一种形式支持它们。可与编程语言互换的数据格式也基于这些结构是有道理的。在 JSON 中,它们采用以下形式:

OBJECT:

目的:

An?object?is an unordered set of name/value pairs. An object begins with?{?(left brace)?and ends with?}?(right brace). Each name is followed by?:?(colon)?and the name/value pairs are separated by?,?(comma).

对象是一组无序的名称/值对。一个对象以?{?(左大括号)? 开始并以?}?(右大括号)结束。每个名称后跟?:?(冒号)?,名称/值对由?,?(逗号)分隔。

{string: value, string: value}

ARRAY:

大批:

An?array?is an ordered collection of values. An array begins with?[?(left bracket)?and ends with?]?(right bracket). Values are separated by?,?(comma).

数组是有序的值集合。数组以?[?(左方括号)? 开始,以?]?(右方括号)结束。值由?,?(逗号)分隔。

[value, value, value ….]

VALUE:

价值:

A?value?can be a?string?in double quotes, or a?number, or?true?or?false?or?null, or an?object?or an?array. These structures can be nested.

A?value?可以是双引号中的?string?,或a?number,或?true?or?false?or?null,或an?object?或an?array。这些结构可以嵌套。

STRING:

细绳:

A?string?is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

A?string? 是零个或多个 Unicode 字符的序列,用双引号括起来,使用反斜杠转义。一个字符表示为单个字符串。字符串非常类似于 C 或 Java 字符串。

NUMBER:

数字:

A?number?is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

除了不使用八进制和十六进制格式之外,A?number? 非常类似于 C 或 Java 数字。

ABOUT WHITESPACE:

关于空格:

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.

可以在任何一对标记之间插入空格。除了一些编码细节,这完全描述了语言。