{名称1:值,名称2:值2}
]
5 )元素值可具有的类型:string, number, object, array, true, false, null
2.json中的五种写法:
1)传统方式存储数据,调用数据
<script type="text/javascript"> //JS传统方式下定义"类" function Person(id,name,age){ this.id = id; this.name = name; this.age = age; } //JS传统方式下创建"对象" var p = new Person(20141028,"一叶扁舟",22); //调用类中的属性,显示该Person的信息 window.alert(p.id); window.alert(p.name); window.alert(p.age); </script>
<script type="text/javascript"> var person = { id:001, name:"一叶扁舟", age:23 } window.alert("编号:"+person.id); window.alert("用户名:"+person.name); window.alert("年龄:"+person.age); </script>
<script type="text/javascript"> var p = [ {id:001,name:"一叶扁舟",age:22}, {id:002,name:"无悔",age:23}, {id:003,name:"无悔_一叶扁舟",age:24} ]; for(var i = 0; i < p.length; i++){ window.alert("编号:"+p[i].id); window.alert("用户名:"+p[i].name); window.alert("年龄:"+p[i].age); } </script>
<script type="text/javascript"> var p = { "province":[ {"city":"福州"}, {"city":"厦门"}, {"city":"莆田"} ] }; window.alert("所在城市:" + p.province[0].city); </script>
<script type="text/javascript"> var p = { "ids":[ {"id":001}, {"id":002}, {"id":003} ], "names":[ {"name":"一叶扁舟"}, {"name":"无悔"}, {"name":"无悔_一叶扁舟"} ] }; for(var i = 0; i < p.names.length; i++){ window.alert("名字:"+p.names[i].name); } for(var i = 0; i < p.ids.length; i++){ window.alert("id:"+p.ids[i].id); } </script>
<script type="text/javascript"> var p = { "province":["福州","厦门","莆田"] }; window.alert("城市的个数:"+p.province.length); window.alert("分别是:\n"); for(var i=0;i<p.province.length;i++){ window.alert(p.province[i]); } </script>
原文地址:http://blog.csdn.net/u011662320/article/details/40541363