문제

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

도움이 되었습니까?

해결책

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

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.

  • 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."

다른 팁

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

This is from the ECMAScript specification.

JSONText :
    JSONValue

JSONValue :
    JSONNullLiteral 
    JSONBooleanLiteral 
    JSONObject 
    JSONArray 
    JSONString 
    JSONNumber

yes, try it out here.

http://www.jsonlint.com/

and put in [{}]

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top