菜单切换
FACESOHO知行者
心灵
记录
远方
赞赏工具
源代码:
点击运行
保存
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>小鸟启蒙(facesoho.com)</title> </head> <body> <p>JSON 读取数据实例</p> <p id="demo"></p> <script> var stu = {"student": //stu 对象包含student的key,值为一个数组 [ //数组的每一个值为一个具体的学生对象 {"name": "Tom","Grade":1, "age":11, "gender": "M"}, //学生对象的键为名字,值为对应属性 {"name": "Jerry", "Grade":1, "age":10, "gender": "M"} //每个属性对应的是一个key,value对 ], "classroom": {"class1": "room1", "class2": "room2"} //对象的值,嵌套对象 }; document.write(stu.student[1].name + "<br>"); // 输出第二个学生名 document.write(stu.student[0].age + "<br>"); // 输出第一个学生年龄 document.write(stu.classroom.class1 + "<br>"); // 输出 classroom 的 class1 值 document.write(stu["classroom"].class2 + "<br>"); // 也可用中括号键访问对象值 </script> </body> </html>
运行结果