javascript 对象 vs 数组 vs JSON

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

javascript objects vs arrays vs JSON

javascriptjson

提问by Octavient

Despite much googling and hair-pulling, I can't for the life of me articulate the difference between json, objects, and arrays (in javascript). Below is how I've been using 2-dimensional data containers (afraid to use the words "array," "object," or "json" here). Please tell me what these two examples are?

尽管有很多谷歌搜索和头发拉扯,但我终生无法阐明 json、对象和数组(在 javascript 中)之间的区别。下面是我使用二维数据容器的方式(这里害怕使用“数组”、“对象”或“json”这些词)。请告诉我这两个例子是什么?

//first example:
[
    {"record_id":1,"name":"Frank"},
    {"record_id":2,"name":"Sally"}
]

//second example:
{
"countries": 
    [
    {"id":1,"name":"Canada"},
    {"id":2,"name":"Mexico"}
    ],
"states":
    [
    "id":1,"name":"Maine"},
    {"id":2,"name":"Alaska"}
    ]
}

回答by Gary G

JSON is a representation of the data structure, it's not an object or an array.

JSON 是数据结构的表示,它不是对象或数组。

[1,2,3]

is an array.

是一个数组。

{"foo":"bar"}

is an object.

是一个对象。

In your example,

在你的例子中,

[
  {"record_id":1,"name":"Frank"},
  {"record_id":2,"name":"Sally"}
]

Is an array of objects.

是一个对象数组。

{
  "countries": 
    [
      {"id":1,"name":"Canada"},
      {"id":2,"name":"Mexico"}
    ],
  "states":
    [
      {"id":1,"name":"Maine"},
      {"id":2,"name":"Alaska"}
    ]
}

Is an object containing other arrays and objects inside of it.

是一个包含其他数组和对象的对象。

回答by gahooa

JSONis JavaScript Object Notation. This is simply a way of writing down JavaScript data types. It is not a data type in-and-of-itself.

JSONJavaScript Object Notation. 这只是写下 JavaScript 数据类型的一种方式。它本身不是一种数据类型。

See below for some examples of JavaScript data types, and the literal notation of creating them.

有关 JavaScript 数据类型的一些示例,以及创建它们的文字表示法,请参见下文。

JSON can be used to send data from the server to the browser, for example, because it is easy for JavaScript to parse into a normal JavaScript data structure.

例如,JSON 可用于将数据从服务器发送到浏览器,因为 JavaScript 很容易解析为正常的 JavaScript 数据结构。

In your example, you are using lists of objects, and objects of objects.

在您的示例中,您使用的是对象列表和对象的对象。

This is a list of 3 empty objects.

这是一个包含 3 个空对象的列表。

[{}, {}, {}]

This is a list of three simple records:

这是三个简单记录的列表:

var mylist = [
    {name: 'John', age: 24},
    {name: 'Bill', age: 42},
    {name: 'Jill', age: 18},
    ]

You can access it like this:

您可以像这样访问它:

mylist[1].name
>>> 'Bill'

mylist[2].age
>>> 18

JavaScript has several data types:

JavaScript 有几种数据类型:

Number

数字

1
100
-2000
123.45

String

细绳

"Hi John"
"Message:\nGo Forth"

Boolean

布尔值

true
false

Array

大批

[1,2,3]
[]
["a", "b", 123]
["a", "b", 123, [3,4,5]]

Object

目的

{}
{a: 10}
{mylist: [1,2,3], yourlist: [4,5,6]}
{myself: {name: 'me', age: 10}, yourself: {name: 'you', age: 20}}

回答by jahroy

You use {braces }to declare an object literal.

您可以使用{大括号}来声明对象字面量

You use [square brackets ]to declare an array literal.

使用[方括号]声明数组文字

Objects are collections of key name value pairs.

对象是键名值对的集合。

Here's an example of an array of strings:

下面是一个字符串数组的例子:

var a = [ "one", "two", "three" ];

Here's an example of a simple object that represents a person:

下面是一个代表一个人的简单对象的例子:

var personObject = {
    name: 'Joe',
    age: 25,
    hometown: 'New York'        
};

回答by user123444555621

JSON is a textual data interchange format. As its name ("JavaScript Object Notation") suggests, it originates from JS; meaning that JSON is actually syntactically valid JavaScript. In other words, you can paste a JSON string directly into your JS code.

JSON 是一种文本数据交换格式。顾名思义(“JavaScript Object Notation”),它起源于 JS;这意味着 JSON 实际上是语法上有效的 JavaScript。换句话说,您可以将 JSON 字符串直接粘贴到您的 JS 代码中。

Arrays are special Objects. They can be constructed by [].

数组是特殊的对象。它们可以由 构建[]

Objects can be constructed via {}.

对象可以通过{}.

So what you have in your example is two JSON strings, one representing an Array of objects, the second one representing an Object whose properties are themselves Arrays of Objects.

因此,您在示例中拥有的是两个 JSON 字符串,一个表示对象数组,第二个表示一个对象,其属性本身就是对象数组。

回答by Johndave Decano

Well i believe objects can have methods and properties while arrays cant. JSON can be passed to the server while array cant be, unless you pass it as a string by POST

好吧,我相信对象可以有方法和属性,而数组不能。JSON 可以传递到服务器,而数组不能传递,除非您通过 POST 将其作为字符串传递