JSON 数组
1.访问数组
1.一次访问一个嵌套内容值
var myObj, x;
myObj = {
"name":"网站",
"num":,
"sites":[ "Google", "Runoob", "Taobao" ]
}
x = myObj.sites[];
document.getElementById("demo").innerHTML = x;
输出内容:
Google 2.改进:遍历嵌套内容值
var myObj, x;
myObj = {
"name":"网站",
"num":3,
"sites":[ "Google", "Runoob", "Taobao" ]
}
x = myObj.sites[0];
for(x in myObj.sites){
document.getElementById("demo").innerHTML += myObj.sites[x]+ "<br>";
} 输出内容:
Runoob
Taobao
2.嵌套JSON对象中的数组
<p>循环内嵌数组:</p> <p id="demo"></p> <script> var myObj, i, j, x = "";
myObj = {
"name":"网站",
"num":,
"sites": [
{ "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻译" ] },
{ "name":"Runoob", "info":[ "我是一个菜鸟", "菜鸟的工具", "菜鸟的微信" ] },
{ "name":"Taobao", "info":[ "淘宝", "京东" ] }
]
} 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>";
}
} document.getElementById("demo").innerHTML = x;
注:<br> 是换行的作用
3.修改数组值,删除数组元素和前面一样