json的解析:
json是js原生的内容,也就意味着js可以直接取出json对象中的数据。
案例一:
1 var persons = [ 2 {"firstname":"张","lastname":"三丰","age":100}, 3 {"firstname":"李","lastname":"四","age":65}, 4 ] 5 //取出firstname的李 6 alert(persons[1].firstname); 7 //取出100 8 alert(persons[0].age);
案例二(一个key对应多个数组,一个数组对应多个对象的案例):
1 var json = { 2 "baobao":[ 3 {"name":"小双","age":28,"addr":"扬州"}, 4 {"name":"建宁","age":18,"addr":"紫禁城"}, 5 {"name":"阿珂","age":10,"addr":"山西"}, 6 7 ] 8 9 }; 10 11 //娶建宁 12 alert(json.baobao[1].name); 13 //娶小双 14 alert(json.baobao[0].name) 15 //山西 16 alert(json.baobao[2].addr)