JSON 数组

JSON 教程 · 2019-04-08 19:39:12

数组作为 JSON 对象

实例

[ "Google", "Codercto", "Taobao" ]

JSON 数组在中括号中书写。

JSON 中数组值必须是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。

JavaScript 中,数组值可以是以上的 JSON 数据类型,也可以是 JavaScript 的表达式,包括函数,日期,及 undefined

JSON 对象中的数组

对象属性的值可以是一个数组:

实例

{ "name":"网站", "num":3, "sites":[ "Google", "Codercto", "Taobao" ] }

我们可以使用索引值来访问数组:

实例

x = myObj.sites[0];

循环数组

你可以使用 for-in 来访问数组:

实例

for (i in myObj.sites) { x += myObj.sites[i] + "<br>"; }

你也可以使用 for 循环:

实例

for (i = 0; i < myObj.sites.length; i++) { x += myObj.sites[i] + "<br>"; }

嵌套 JSON 对象中的数组

JSON 对象中数组可以包含另外一个数组,或者另外一个 JSON 对象:

实例

myObj = { "name":"网站", "num":3, "sites": [ { "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻译" ] }, { "name":"Codercto", "info":[ "码农教程", "码农工具", "码农微信" ] }, { "name":"Taobao", "info":[ "淘宝", "网购" ] } ] }

我们可以使用 for-in 来循环访问每个数组:

实例

for (i in myObj.sites) { x += "<h1>" + myObj.sites[i].name + "</h1>"; for (j in myObj.sites[i].info) { x += myObj.sites[i].info[j] + "<br>"; } }

修改数组值

你可以使用索引值来修改数组值:

实例

myObj.sites[1] = "Github";

删除数组元素

我们可以使用 delete 关键字来删除数组元素:

实例

delete myObj.sites[1];

点击查看所有 JSON 教程 文章: https://www.codercto.com/courses/l/45.html

查看所有标签

Hacking

Hacking

Jon Erickson / No Starch Press / 2008-2-4 / USD 49.95

While other books merely show how to run existing exploits, Hacking: The Art of Exploitation broke ground as the first book to explain how hacking and software exploits work and how readers could deve......一起来看看 《Hacking》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具