直接看图看代码!代码中有具体的案例!
testJSON.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>测试JSON</title>
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/css/body.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/css/mark.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/css/input2.css">
</head>
<body οnlοad="testJSON()">
<center>
<form action="">
<h2>测试JSON</h2>
<h3>Ajax中解析Json的两种方法详解</h3>
<h3>顺便测试下javascript局部变量和全局变量同名的问题</h3>
<a target="_blank" href="https://www.cnblogs.com/mylove103104/p/4599001.html">
可参考网页https://www.cnblogs.com/mylove103104/p/4599001.html
</a><br><br>
共有<input type="text" id="count" name="count" value="">条记录<br>
<br> 共有<input type="text" id="pageCount" name="pageCount"
value="">页<br>
<br> 第<input type="text" id="nowPage" name="nowPage" value="">页<br>
<br>
用户名<input type="text" id="userName" value="我叫张三"><br><br>
手机号<input type="text" id="userTel" value="13755668899"><br><br>
<input type="text" id="testJsonData"><br><br>
<input type="text" id="testJsonObj"><br><br>
<input type="text" id="testJsonObj2"><br><br>
<input type="text" id="testJsonDataStr"><br><br>
<input type="button" value="测试javascript函数名和变量名同名" οnclick="test()">
<input type="button" value="测试调用函数时把js变量当成函数的参数来传递" οnclick="test3(hometown)">
<input type="button" value="测试javascript局部变量和全局变量同名" οnclick="testVariable()">
<input type="button" value="测试json" οnclick="testJSON2()">
<input type="button" value="测试jsonStr" οnclick="testJsonStr()">
</form>
</center>
</body>
<script type="text/javascript">
var userName = "zhangsan";//全局变量
var test = "test ok";
var test;
var hometown = "江西省赣州市于都县!";
function test(){
alert("test=" + test);
}
function test2(){
alert("test=" + test);
}
function test3(home){
alert("home=" + home + "test=" + test);
}
function testJSON(){
var jsonDate = '{ "count":"66","pageCount":7,"nowPage":"2" }';
var jsonObj = eval( '(' + jsonDate + ')' ); // eval();方法
document.getElementById("nowPage").value = jsonObj.nowPage;
var jsonObj = JSON.parse( jsonDate ); // JSON.parse(); 方法
document.getElementById("count").value = jsonObj.count;
var jsonDate = '{ "count":alert("hello javaScript"),"pageCount":7,"nowPage":23 }';
var jsonObj = eval( '(' + jsonDate + ')' ); // eval();方法
var jsonDate = '{ "count":7,"pageCount":"testFor()","nowPage":23 }';
var jsonObj = eval( '(' + jsonDate + ')' ); // eval();方法
var jsonObj = JSON.parse( jsonDate ); // JSON.parse(); 方法
document.getElementById("pageCount").value = jsonObj.pageCount;
var jsonDate = '{ "count":testFor(),"pageCount":7,"nowPage":23 }';
var jsonObj = eval( '(' + jsonDate + ')' ); // eval();方法
//报错 这个错误告诉我们这个字符串是不合法的(具体可参考https://www.cnblogs.com/mylove103104/p/4599001.html)
var jsonObj = JSON.parse( jsonDate ); // JSON.parse(); 方法
}
function testJSON2(){
/*
使用JSON.parse()函数,需严格遵守JSON规范,如属性都需用引号引起来,我在火狐浏览器下测试的结果是属性必须使用双引号引起来,属
性不能使用单引号引起来,使用单引号把属性引起来会报错,所以只能使用双引号把属性引起来。具体可以参考网
页http://www.jb51.net/article/25987.htm
*/
//var jsonDate = '{ "count":"198","pageCount":20,"nowPage":"8" }';
//var jsonDate = '{ "count":"269","pageCount":89,"nowPage":"26" }';
//var jsonDate = "{ 'count':504,'pageCount':233,'nowPage':'152' }";
//var obj = JSON.parse(jsonDate);
//var obj = eval("(" + jsonDate + ")");
//var obj = eval(jsonDate);
//var jsonDate = "{ 'count':'668','pageCount':'999','nowPage':'2876' }";
//var jsonDate = "{ 'count':1399,'pageCount':558,'nowPage':689257 }";
//var jsonDate = '{ count:226,pageCount:9987,nowPage:75589 }';
var jsonDate = '{ "count":"12659","pageCount":1198,"nowPage":3357 }';
//JSON.parse()函数对json的语法比较严格
var obj = JSON.parse(jsonDate);
//var obj = eval("(" + jsonDate + ")");
document.getElementById("testJsonData").value = obj + " - " + obj.count + " - " + obj.pageCount;
//
// 员工信息
var employeeInfo = {
emplNum : '123',
emplName : 'lee',
telNum : '18888888888'
};
// 标签信息
var dataParam = [];
for(var i=0; i<3; i++){
var employeeLabel = {
labelName : 'name' + i,
labelOrder : i,
labelRemarks : 'remark' + i
}
dataParam.push(employeeLabel);
}
//用户信息
var userInfo = {
userNum : '9527',
userName : document.getElementById("userName").value,
userTelNum : document.getElementById("userTel").value
};
document.getElementById("testJsonObj").value = employeeInfo.telNum + " - " + dataParam[0].labelName;
document.getElementById("testJsonObj2").value = userInfo.userNum + " - " + userInfo.userName + " - " + userInfo.userTelNum;
document.getElementById("testJsonDataStr").value = JSON.stringify(employeeInfo);
}
function testJsonStr(){
var params = [];
for(var i = 0; i < 3; i++){
var param = [];
param.push("one");
param.push("two");
param.push("three");
params.push({"group":i,"param":param});
}
var json = JSON.stringify(params);
alert(json);
}
//测试javascript中局部变量和全局变量同名
function testVariable(){
alert("测试1-userName=" + userName);
var userName = "lisi";
alert("测试2-userName=" + userName);
alert("测试3-userName=" + this.userName);
}
function testFor(){
for (var i = 1; i <= 3; i++) {
alert("hello" + i);
}
}
</script>
</html>