<body>
<div id="demo"></div>
<script>
/*方法一:使用点号(.)来访问 JSON 对象的值*/
var myObj, x; //定义变量
myObj = { "name":"runoob", "alexa":10000, "site":null };
x = myObj.name;
document.getElementById("demo").innerHTML = x;
/*方法二:使用中括号([])来访问属性的值*/
var myObj, x;
myObj = { "name":"runoob", "alexa":10000, "site":null };
x = myObj["name"];
document.getElementById("demo").innerHTML = x;
/*for in遍历获取属性*/
var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) { document.getElementById("demo").innerHTML += x + "<br>"; }
/*for in遍历获取属性值*/
//在使用for遍历时,只能通过 myObj[x] 来获取相应属性的值,而不能使用 myObj.x
var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) {
document.getElementById("demo").innerHTML += myObj[x] + "<br>";
}
</script>
</body>