[ "Google", "Runoob", "Taobao" ]
JSON 數組在中括号中書寫。
JSON 中數組值必須是合法的 JSON 資料類型(字元串, 數字, 對象, 數組, 布爾值或 null)。
JavaScript 中,數組值可以是以上的 JSON 資料類型,也可以是 JavaScript 的表達式,包括函數,日期,及 undefined。
對象屬性的值可以是一個數組:
{
"name":"網站",
"num":3,
"sites":[ "Google", "Runoob", "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++) {
JSON 對象中數組可以包含另外一個數組,或者另外一個 JSON 對象:
myObj = {
"sites": [
{ "name":"Google", "info":[ "Android", "Google 搜尋", "Google 翻譯" ] },
{ "name":"Runoob", "info":[ "菜鳥教程", "菜鳥工具", "菜鳥微信" ] },
{ "name":"Taobao", "info":[ "淘寶", "網購" ] }
]
我們可以使用 for-in 來循環通路每個數組:
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";
我們可以使用 <b>delete</b> 關鍵字來删除數組元素:
delete myObj.sites[1];