数组可以是顶级 JSON 文本吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3833299/
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:34:51  来源:igfitidea点击:

Can an array be top-level JSON-text?

arraysjsonrfc4627

提问by Dustin Getz

per the debate in this post: json-conversion-in-javascript

根据这篇文章中的辩论:json-conversion-in-javascript

回答by Dustin Getz

Yes, an array is legal as top-level JSON-text.

是的,数组作为顶级 JSON 文本是合法的。

There are three standard documents defining JSON: RFC 4627, RFC 7159(which obsoletes RFC 4627), and ECMA-404. They differ in which top-level elements they allow, but all allow an object or an array as the top-level element.

定义 JSON 的标准文档有三个:RFC 4627RFC 7159(废弃 RFC 4627)和ECMA-404。它们的区别在于它们允许哪些顶级元素,但都允许对象或数组作为顶级元素。

  • RFC 4627: Object or array.
    "A JSON text is a serialized object or array."
  • RFC 7159: Any JSON value.
    "A JSON text is a serialized value."
  • ECMA-404: Any JSON value.
    "A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar."
  • RFC 4627:对象或数组。
    “JSON 文本是序列化的对象或数组。”
  • RFC 7159:任何 JSON 值。
    “JSON 文本是一个序列化值。”
  • ECMA-404:任何 JSON 值。
    “JSON 文本是由符合 JSON 值语法的 Unicode 代码点形成的一系列标记。”

回答by Matthew Flaschen

Yes, but you should consider making the root an object instead in some scenarios, due to JSON hiHymaning. This is an information disclosure vulnerability based on overriding the array constructor in JavaScript.

是的,但由于JSON 劫持,您应该考虑在某些情况下将根设为对象。这是一个基于 JavaScript 中覆盖数组构造函数的信息泄露漏洞。

回答by ChaosPandion

This is from the ECMAScript specification.

这是来自 ECMAScript 规范。

JSONText :
    JSONValue

JSONValue :
    JSONNullLiteral 
    JSONBooleanLiteral 
    JSONObject 
    JSONArray 
    JSONString 
    JSONNumber

回答by hvgotcodes

yes, try it out here.

是的,在这里试试。

http://www.jsonlint.com/

http://www.jsonlint.com/

and put in [{}]

并放入 [{}]

回答by cdunn2001

There is some confusion, seen in the other comments. The "application/json" media type allows only object or array at the top-level for JSON-text, per JSON RFC. However, for a parser any JSON value is acceptable, as seen in the ECMAScript specification.

有一些混乱,在其他评论中看到。根据JSON RFC,“application/json”媒体类型仅允许 JSON 文本的顶级对象或数组。但是,对于解析器,任何 JSON 值都是可以接受的,如 ECMAScript 规范中所见。