学习Json
学习Json,写了一个简单的json ,然后写了三个遍历的方法,感觉第二种json的结构稍微好一点。刚刚开始用,还需要很大改进啊!下面是测试代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>学习JSON</title></head>
<script>
var value = {
"china":{
"hangzhou":{"item":"1"},
"shanghai":{"item":"2"},
"chengdu":{"item":"3"}
},
"America":{
"aa":{"item":"1"},
"bb":{"item":"2"}
},
"Spain":{
"dd":{"item":"1"},
"ee":{"item":"2"},
"ff":{"item":"3"}
}
};
var value2 = {
"china":[
{"name":"hangzhou", "item":"1"},
{"name":"shanghai", "item":"2"},
{"name":"sichuan", "item":"3"}
],
"America":[
{"name":"aa", "item":"1"},
{"name":"bb", "item":"2"}
],
"Spain":[
{"name":"cc", "item":"1"},
{"name":"dd", "item":"2"},
{"name":"ee", "item":"3"}
]
};
start = new Date().getTime();
for(countryObj in value) {
document.write(countryObj + ": <br />");
for(cityObj in value[countryObj]) {
document.write(" "+ cityObj + " item: " + value[countryObj][cityObj]["item"] + "<br />");
}
}
end = new Date().getTime();
document.write("耗时:"+(end - start) / 1000 + "sec <br />");
start = new Date().getTime();
//for in 遍历第二种json
for(countryObj2 in value2) {
document.write(countryObj2 + ": <br />");
for(cityObj2 in value2[countryObj2]) {
document.write(" "+ value2[countryObj2][cityObj2]["name"]
+ " item:" + value2[countryObj2][cityObj2]["item"] +"<br />");
}
}
end = new Date().getTime();
document.write("<br />耗时:"+(end - start) / 1000 + "sec <br />");
start = new Date().getTime();
//for in 结合数组遍历第二中数组
for(countryObj2 in value2) {
document.write(countryObj2 + ": <br />");
for(var i = 0; i < value2[countryObj2].length; i++) {
document.write(" "+ value2[countryObj2][i]["name"]
+ " item:" + value2[countryObj2][i]["item"] +"<br />");
}
}
end = new Date().getTime();
document.write("<br />耗时:"+(end - start) / 1000 + "sec <br />");
</script>
<body></body>
</html>
<html><head><title>学习JSON</title></head>
<script>
var value = {
"china":{
"hangzhou":{"item":"1"},
"shanghai":{"item":"2"},
"chengdu":{"item":"3"}
},
"America":{
"aa":{"item":"1"},
"bb":{"item":"2"}
},
"Spain":{
"dd":{"item":"1"},
"ee":{"item":"2"},
"ff":{"item":"3"}
}
};
var value2 = {
"china":[
{"name":"hangzhou", "item":"1"},
{"name":"shanghai", "item":"2"},
{"name":"sichuan", "item":"3"}
],
"America":[
{"name":"aa", "item":"1"},
{"name":"bb", "item":"2"}
],
"Spain":[
{"name":"cc", "item":"1"},
{"name":"dd", "item":"2"},
{"name":"ee", "item":"3"}
]
};
start = new Date().getTime();
for(countryObj in value) {
document.write(countryObj + ": <br />");
for(cityObj in value[countryObj]) {
document.write(" "+ cityObj + " item: " + value[countryObj][cityObj]["item"] + "<br />");
}
}
end = new Date().getTime();
document.write("耗时:"+(end - start) / 1000 + "sec <br />");
start = new Date().getTime();
//for in 遍历第二种json
for(countryObj2 in value2) {
document.write(countryObj2 + ": <br />");
for(cityObj2 in value2[countryObj2]) {
document.write(" "+ value2[countryObj2][cityObj2]["name"]
+ " item:" + value2[countryObj2][cityObj2]["item"] +"<br />");
}
}
end = new Date().getTime();
document.write("<br />耗时:"+(end - start) / 1000 + "sec <br />");
start = new Date().getTime();
//for in 结合数组遍历第二中数组
for(countryObj2 in value2) {
document.write(countryObj2 + ": <br />");
for(var i = 0; i < value2[countryObj2].length; i++) {
document.write(" "+ value2[countryObj2][i]["name"]
+ " item:" + value2[countryObj2][i]["item"] +"<br />");
}
}
end = new Date().getTime();
document.write("<br />耗时:"+(end - start) / 1000 + "sec <br />");
</script>
<body></body>
</html>
浙公网安备 33010602011771号