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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
onload = function()
{
//json
var str = {'name':'caohuan','age':21};
//alert(str.name);
//json数组
var str1 = [{'name':'caohuan','age':21},{'name':'文斌','age':55}];
//alert(str1[1].name);
//将字符窜转换成json
var str2 = '1+2';
var json = eval(str2);
//alert(str2);
//alert(json);
//将字符窜格式的json转换成json
var str3 = '{"name":"曹欢","age":23}';
//千万记得json字符存格式的数组转换成json格式的数组的转化的方式
var json1 = eval("("+str3+")");
//alert(json1.name);
//这是将json字符窜格式的数组转换成json数组
var str4 = '[{"name":"曹欢","age":23},{"name":"文斌","age":"34"}]';
var json2 = eval("("+str4+")");
alert(json2[1].age);
}
</script>
</head>

<body>
This is my JSP page. <br>
</body>
</html>

你可能感兴趣的:(json)