JSON中的*值必须是数组还是对象?

时间:2022-12-30 20:21:38

This object literal is valid JSON:

这个对象文字是有效的JSON:

{ "foo" : "bar"}

And so is this array literal:

这个数组也是字面的:

["foo", "bar", 1, 2, 3, null]

But what about a string by itself?

但是弦本身呢?

"foo"

Or a number by itself?

还是一个单独的数字?

 0

2 个解决方案

#1


2  

Nowadays: No, top level entities do not have to be an array or object.

现在:不,顶层实体不必是数组或对象。

Recent JSON specs (RFC 7159 Mar 2014, and ECMA-404) define a JSON text as optional whitespace surrounding any JSON value, where a value is any one of:

最近的JSON规范(RFC 7159 Mar 2014和ECMA-404)将JSON文本定义为围绕任何JSON值的可选空格,其中值为:

  • object
  • 对象
  • array
  • 数组
  • string
  • 字符串
  • number
  • 数量
  • true
  • 真正的
  • false
  • null

(see the start of sections 2. JSON Grammar, and 3. Values)

(见第2节的开头。JSON的语法,和3。值)

Older JSON specs (RFC 4627, obsoleted) only allowed the top-level element to be an object or an array.

旧的JSON规范(RFC 4627,废弃)只允许*元素是对象或数组。

#2


-1  

Short answer is Yes.
JSON is used to represent object as string and convert string to object.
So you if you don't need to convert string to object or array then this is not related to JSON.

简短的回答是肯定的。JSON用于将对象表示为字符串,并将字符串转换为对象。如果不需要将字符串转换为对象或数组,那么这与JSON无关。

#1


2  

Nowadays: No, top level entities do not have to be an array or object.

现在:不,顶层实体不必是数组或对象。

Recent JSON specs (RFC 7159 Mar 2014, and ECMA-404) define a JSON text as optional whitespace surrounding any JSON value, where a value is any one of:

最近的JSON规范(RFC 7159 Mar 2014和ECMA-404)将JSON文本定义为围绕任何JSON值的可选空格,其中值为:

  • object
  • 对象
  • array
  • 数组
  • string
  • 字符串
  • number
  • 数量
  • true
  • 真正的
  • false
  • null

(see the start of sections 2. JSON Grammar, and 3. Values)

(见第2节的开头。JSON的语法,和3。值)

Older JSON specs (RFC 4627, obsoleted) only allowed the top-level element to be an object or an array.

旧的JSON规范(RFC 4627,废弃)只允许*元素是对象或数组。

#2


-1  

Short answer is Yes.
JSON is used to represent object as string and convert string to object.
So you if you don't need to convert string to object or array then this is not related to JSON.

简短的回答是肯定的。JSON用于将对象表示为字符串,并将字符串转换为对象。如果不需要将字符串转换为对象或数组,那么这与JSON无关。