`
孔雀王子
  • 浏览: 40826 次
  • 性别: Icon_minigender_1
  • 来自: 河北
文章分类
社区版块
存档分类
最新评论

JS+Ajax解析JSON的案例

阅读更多

        JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写,同时也易于机器解析和生成。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。

        JSON构建的结构:

        1. “名称/值"对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。

        2. 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

        以下是我写的一个Js+Ajax解析Json的案例。源代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<style type="text/css">
td {
	text-align: center;
	color: #333333;
	font-size: 12px;
}

th {
	color: #aaaaaa;
	font-size: 13px;
}
</style>

		<script type="text/javascript">
function getXMLHttpRequest() {

	var xhr;

	try {

		xhr = new XMLHttpRequest();

	} catch (err1) {

		try {

			xhr = new ActiveXObject("Microsoft.XMLHTTP");

		} catch (err2) {

			alert("您的浏览器版本不支持Ajax....");

		}

	}

	return xhr;

}

function $(id) {

	return document.getElementById(id);

}

function saxJson() {

	var xhr = getXMLHttpRequest();

	xhr.open("GET", "./json.jsp", true);

	xhr.send();

	xhr.onreadystatechange = function() {

		if (xhr.readyState == 4 && xhr.status == 200) {

			var stus = eval("(" + xhr.responseText + ")");

			var tby = $("tby");

			for ( var i = 0; i < stus.length; i++) {

				var tr = document.createElement("tr");

				var ntd = document.createElement("td");

				var std = document.createElement("td");

				var atd = document.createElement("td");

				var etd = document.createElement("td");

				ntd.innerHTML = stus[i].name;

				std.innerHTML = stus[i].sex;

				atd.innerHTML = stus[i].age;

				etd.innerHTML = stus[i].email;

				tr.appendChild(ntd);

				tr.appendChild(std);

				tr.appendChild(atd);

				tr.appendChild(etd);

				tby.appendChild(tr);

			}

		}

	}

}
</script>
	</head>

	<body onload="saxJson();">

		<h2 align="center" style="color: #666666;">
			JS解析JSON的案例
		</h2>
		<div id="display" align="center">
			<table style="border-collapse: collapse" cellspacing="0"
				cellpadding="3" id="tbl" border="1" width="360">
				<thead>
					<tr>
						<th>
							姓名
						</th>
						<th>
							性别
						</th>
						<th>
							年龄
						</th>
						<th>
							邮箱
						</th>
					</tr>
				</thead>
				<tbody id="tby">

				</tbody>
			</table>
		</div>
		<br />
		<div align="center" style="font-size: 12px; color: #333333;">
			Copyright&copyCSDN Corporation 2010-2011
		</div>
	</body>
</html>

 json.jsp代码清单:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
student=[

	{
		name:"SXPGOG",
		
		sex:"boy",
		
		age:"21",
		
		email:"dyzh.yysg@163.com"
	
	},
	
	{
	
		name:"Lily",
		
		sex:"Girl",
		
		age:"19",
		
		email:"532265808@qq.com"
	
	},
	
	{
	
		name:"XYZ",
		
		sex:"Boy",
		
		age:"22",
		
		email:"unknow@qq.com"
	
	},
	
	{
	
		name:"Tom",
		
		sex:"Boy",
		
		age:"22",
		
		email:"unknow@qq.com"
	
	}

]

       注意:json.jsp中的代码要符合Json的语法规范

分享到:
评论
5 楼 GZQ0821 2012-07-18  
我的xhr.status返回500,是什么意思???
4 楼 yinghuayu1324117 2011-04-02  
我们只是拿它做个例子而已
3 楼 ekian 2011-04-01  
受教了。没试过直接在JSP页面上写Json串。不过,一般也不会在JSP上写,都是在后台把数据转换成Json格式的数据,在发送到JSP页面上的。。。
2 楼 辽东小小 2011-03-31  
java代码怎么写json啊?楼主写的是js中创建的json串
1 楼 yinghuayu1324117 2011-03-29  
不错嘛

相关推荐

Global site tag (gtag.js) - Google Analytics