json(javascript Object notation )轻量级的javascript传输对象
json的常见的数据格式
1:简单对象格式
var jsonObj={id:1,name:'张三',pass:'111111'};
alert(jsonObj.id);
2:数组对象格式
var jsonObjArr = [{id : 1,name : '张三',pass : '111111'},
{id : 1,name : '张三',pass : '111111'},
{id : 1,name : '张三',pass : '111111'},
{id : 1,name : '张三',pass : '111111'}]
alert(jsonObjArr[0].id);
eg:json实现显示数据库的省份表中的省份和城市表中的城市
准备工作:导入所需要的文件
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
前端页面:
<%@ 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>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">
$(function() {
$.ajax({
type : "post",
url : "getInfo?method=province",
success : function(data) {
var result = JSON.parse(data);
result = result.province;
if (result.resultCode==1) {
$("#province").get(0).options.length = result.length + 1;
$("#province").get(0).options[0] = new Option("请选择", 0);
for (var i = 0; i < result.length; i++) {
$("#province").get(0).options[i + 1] = new Option(
result[i].name, result[i].id);
}
// {"province":[{"id":1,"name":"江西"},{"id":2,"name":"江苏"}]
}else{
alert("服务器错误!") ;
}
}
}
});
});
function loadCity(provinceId) {
$.ajax({
type : "post",
url : "getInfo?method=city",
data : "provinceId=" + provinceId,
success : function(data) {
var result = JSON.parse(data);
result = result.citys;
$("#city").get(0).options.length = result.length + 1;
$("#city").get(0).options[0] = new Option("请选择", 0);
for (var i = 0; i < result.length; i++) {
$("#city").get(0).options[i + 1] = new Option(
result[i].name, result[i].id);
}
// {"province":[{"id":1,"name":"江西"},{"id":2,"name":"江苏"}]
}
});
}
</script>
</head>
<body>
省份:
<select id="province" οnchange="loadCity(this.value)"></select> 城市:
<select id="city"></select>
</body>
</html>
业务逻辑servlet部分
package com.zt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import com.zt.dao.ProvinceDao;
import com.zt.dao.impl.ProvinceDaoImpl;
import com.zt.po.City;
import com.zt.po.Province;
public class GetInfoServlet extends HttpServlet {
ProvinceDao provin =null;
@Override
protected void service(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
String methodString=request.getParameter("method");
if (methodString.equals("province")) {
province(request, response);
}
if (methodString.equals("city")) {
city(request, response);
}
}
protected void province(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
List<Province> provinces=provin.findAllProvince();
JSONObject jsonObject=new JSONObject(); //创建一个JSONObject对象
if (provinces.size()>0) {
jsonObject.element("province",provinces);//将provinces存进JSONObject对象中
jsonObject.element("resultCode",1);//将返回结果成功返回1存进JSONObject对象中
}else{
jsonObject.element("resultCode",0);//将返回结果失败返回0存进JSONObject对象中
}
PrintWriter out =response.getWriter();
out.print(jsonObject.toString());//调用JSONObject对象的toString方法,然后输出
out.flush();
out.close();
}
protected void city(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
String provinceIdString=request.getParameter("provinceId");
int province=0;
if (provinceIdString!=null&&!"".equals(provinceIdString)) {
province=Integer.parseInt(provinceIdString);
}
List<City> citys=provin.findAllProvinceCity(province);
JSONObject jsonObject=new JSONObject();
jsonObject.element("citys", citys);
PrintWriter out =response.getWriter();
out.print(jsonObject.toString());
out.flush();
out.close();
}
@Override
public void init() throws ServletException {
provin=new ProvinceDaoImpl();
}
}