一:语法规则
1.数据存在:名称/值 (例:"name":"dabaojian")
2.数据由逗号隔开
3.大括号保存的是对象,中括号保存的是数组
4.值可以为:整型(30),字符串(" "),数组([ ]),对象({ }),逻辑值(true,false),null。数组中可以包含多个对象
二:Json对象
1.示例:{"name":"dabaojian","age":21,"favorites":null}
注:对象必须在{}里面,key必要用双引号,value可以是上述的几种类型;key和value之间用":"区分;每个key/value用";"区分
2.Json对象
var Object,x;
Object={"name":"dabaojian","age":21,"favorites":null};
x=Object.name;等价于x=Object["name"]
结果:dabaojian
对象的循环:
for(x in Object){Object[x]}
嵌套的json对象
Object={"name":"dabaojian",
"age":21,
"favs":{
"fav1":"music",
"fav2":"ball",
"fav3":"movie"}
}
访问值:Object.favs.fav1
修改值:Object.favs.fav1="sleep";
删除值:delete Object.favs.fav1;
3.Json数组
Object={"name":"dabaojian",
"age":21,
"favs":["music","ball","movie"]}
数组访问:Object.favs[0]
数组遍历:1.for(i in Object){x=Object.favs[i]}
2.for(i=0;i<Object.favs.length;i++){}
数组嵌套:
Object={"name":"dabaojian",
"age":21,
"favs":[{"music":["1","2","3"] },
{"movie":["1","2","3"] }
]
}
删除和修改值与对象的相同
4.Json.parse();(将从服务器获取的数据转为js对象)
var obj = JSON.parse('{ "name":"dabaojian", "age":21, "fav":"music" }');
解析数据:Json不能存date类型的数据,需转换后转成date类型;不能存函数(用法可查)