JavaScript 和 JSON 数组

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

JavaScript and JSON array

javascriptjson

提问by ScG

I am trying to understand how a single and multidimensional javascript array would appear in JSON. Can anyone help me with an example for each?

我试图了解单维和多维 javascript 数组将如何出现在 JSON 中。任何人都可以帮我举一个例子吗?

采纳答案by Justin Ethier

Single-dimensional:

一维:

["one", "two", "three"]

Multi-dimensional:

多维:

[["one", "two", "three"],
 ["four", "five", "six"]]

回答by schematic

Single array of primitive integers:

单个原始整数数组:

[1, 1, 2, 3, 5, 8]

[1, 1, 2, 3, 5, 8]

Single array of objects:

单个对象数组:

[
  {
    "title": "hello",
    "msg": "world"
  },
  {
    "title": "stack",
    "msg": "overflow"
  },
  {
    "title": "json",
    "msg": "array"
  },
]

Multidimensional array of primitive integers:

原始整数的多维数组:

[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]

回答by CMS

I think you should know what's the difference between JSON and a JavaScript Object literal, they can look exactly the same, but there are some semantic differences.

我认为您应该知道 JSON 和 JavaScript 对象文字之间的区别是什么,它们看起来完全一样,但存在一些语义差异。

JSON is a language-agnosticdata interchange format, proposedby Douglas Crockford on 2006, its grammar differs from the JavaScript Object literals, basically by allowing only string keysand the values MUST be an object, array, number, string, or one of the literal names: false, trueor null.

JSON 是一种与语言无关的数据交换格式,由 Douglas Crockford 于 2006 年提出,其语法与 JavaScript对象字面量不同,基本上允许字符串键,值必须是对象、数组、数字、字符串或其中之一字面名称:false,truenull.

Speaking about arrays, in JavaScript they can hold any type of value, primitive values like String, Number, Boolean, undefinedor null, and any type of object, even objects with methods, host objects like DOM elements, Dateobjects and so on.

说到数组,在 JavaScript 中,它们可以保存任何类型的值、原始值(如StringNumberBooleanundefinednull)和任何类型的对象,甚至是带有方法的对象、宿主对象(如 DOM 元素、Date对象等)。

The syntax diagrams of JSON arrays and values might help you:

JSON 数组和值的语法图可能对您有所帮助:

Array
(source: json.org)

大批
(来源:json.org

Value
(source: json.org)

价值
(来源:json.org