javascript对象与数组对比JSON [英] javascript objects vs arrays vs JSON

查看:78
本文介绍了javascript对象与数组对比JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管有大量的谷歌搜索和拉扯,我不能为我的生活阐明json,对象和数组(在javascript中)之间的区别。下面是我如何使用二维数据容器(害怕在这里使用数组,对象或json)。请告诉我这两个例子是什么?

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?

//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"}
    ]
}


推荐答案

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

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

[1,2,3]

是一个数组。

{"foo":"bar"}

是一个对象。

在你的例子中,

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

是一个对象数组。

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

这篇关于javascript对象与数组对比JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆