<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/json.js"></script>
</head>
<body >
<span id="name"></span><br>
<span id="discription"></span>
<hr>
<span id="name1"></span><br>
<span id="discription1"></span>
<script type="text/javascript">
var data='{"name":"肖永健","sex":"男"}';
var dataList='[{"name":"肖永健","sex":"男"},{"name":"林文娜","sex":"女"}]';
var obj =JSON.parse(data);
var obj1 = JSON.parse(dataList);
document.getElementById("name").innerHTML =obj.name;
document.getElementById("discription").innerHTML = obj.sex;
document.getElementById("name1").innerHTML =obj1[0].name;
document.getElementById("discription1").innerHTML =obj1[1].name;
</script>
</body>
</html>
输出结果:
需要注意的:
1.官网下载json.js文件http://www.json.org/json.js
2.关键是json字符串和json对象的转换。JSON.parse(); JSON.stringfy();