javacriipt_json_for_in

</pre><pre code_snippet_id="501892" snippet_file_name="blog_20141030_1_9521402" name="code" class="html"><pre name="code" class="html"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title> json </title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
	<script>
		window.onload = function() {
			var oJson = {
				a : 1,
				b : '123',
				c : "json"
			};
			//定義json
			alert(oJson.a);
			var aJsonObject = [{
				a : 1,
				b : 'abcdefg'
			}, {
				c : 123,
				d : "hello"
			}];
			//數組中存放json
			alert(aJsonObject[0]);
			//輸出數字第一個元素
			alert(aJsonObject[1].c);
			//輸出數字第二個元素中的json元素

			//for in
			var attr = "";
			for (attr in oJson) {
				alert(attr + "=" + oJson[attr]);
				//循環輸出json值
			}
		};
	</script>
	<body>
		
	</body>
</html>



發佈了59 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章