1.數(shù)據(jù)在名稱/值對中
2.數(shù)據(jù)由逗號分隔
3.大括號保存對象, 對象可以包含多個?key/value(鍵/值)對
4.中括號保存數(shù)組
對象語法
{ "name":"runoob", "alexa":10000, "site":null }
JSON 對象使用在大括號({})中書寫。
對象可以包含多個?key/value(鍵/值)對。
key 必須是字符串,value 可以是合法的 JSON 數(shù)據(jù)類型(字符串, 數(shù)字, 對象, 數(shù)組, 布爾值或 null)。
key 和 value 中使用冒號(:)分割。
每個 key/value 對使用逗號(,)分割。
嵌套JSON
myObj = { "name":"runoob",?
?????????????????????"alexa":10000,
?????????????????????"sites":?
????????????????????????????????????{ "site1":"www.runoob.com",? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????? "site2":"m.runoob.com",?
?????????????????????????????????????????"site3":"c.runoob.com" }?
}
document.write(myObj.sites.site1);
輸出www.runoob.com
JSON數(shù)組
myObj = {
"name":"網(wǎng)站",
"num":3,
"sites":[ "Google", "Runoob", "Taobao" ]
}
x = myObj.sites[0];
document.write(x);?輸出Google
嵌套JSON對象的數(shù)組
myObj = {
? ? "name":"網(wǎng)站",
? ? "num":3,
? ? "sites": [
? ? ? ? { "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻譯" ] },
? ? ? ? { "name":"Runoob", "info":[ "菜鳥教程", "菜鳥工具", "菜鳥微信" ] },
? ? ? ? { "name":"Taobao", "info":[ "淘寶", "網(wǎng)購" ] }
? ? ]
}