Javascript中的json

<html>
<head>
<title></title>
</head>
<body>
<script>
	//--------------定义一个json数据类型格式-------------------
var jsonTest = {
	"programmers":[
			{"name":"name1","age":31},
			{"name":"name2","age":21},
			{"name":"name3","age":22}
		],
	"authors":[
			{"name":"name1","age":23},
			{"name":"name2","age":24},
			{"name":"name3","age":25}
		]
};
function show_json_data(){
	var programmers = "";
	for(var i = 0 ; i < jsonTest.programmers.length ; i++){
		programmers += "jsonTest.programmers["+i+"]:name="+jsonTest.programmers[i].name + ",age=" + jsonTest.programmers[i].age + "\n";
	}
	
	var authors = "";
	for(var i = 0 ; i < jsonTest.authors.length ; i++){
		authors += "jsonTest.authors["+i+"]:name="+jsonTest.authors[i].name + ",age=" + jsonTest.authors[i].age + "\n";
	}
	alert(programmers + "\n" + authors);
}

function change_json_data(){
	alert('before change----->first_name='+jsonTest.authors[0].name + ",age=" + jsonTest.authors[0].age);
	alert('change....');
	jsonTest.authors[0].name = "opqrst hou";
	jsonTest.authors[0].age = 24;
	alert('after change----->first_name='+jsonTest.authors[0].name + ",age=" + jsonTest.authors[0].age);
}

function jsonToString(){
	alert();
}
</script>
<body>
	<input type="button" value="显示json数据" onclick="show_json_data();"/>
	<input type="button" value="修改json数据" onclick="change_json_data();"/>
	<input type="button" value="json转为字符串" onclick="jsonToString();"/><br/>
	更详细内容:http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/
</body>
</html>
posted @ 2011-01-17 15:24  opqrst  阅读(117)  评论(0)    收藏  举报