欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSON解析 博客分类: web前端 jsonajaxjson-lib 

程序员文章站 2024-02-24 16:18:10
...
AJAX里面的JSON解析

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("GBK");
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = response.getWriter();
		
		String str = "[" +
				"{name:'吉林省'}," +
				"{name:'辽宁省'}," +
				"{name:'山东省'}" +
				"]";
		out.println(str);
		out.flush();
		out.close();
	}

js文件内容:
window.onload = function() {
	var req = getXMLHttpRequest();
	
	req.onreadystatechange = function() {
		if(req.readyState == 4) {
			if(req.status == 200 || req.status == 304) {
				var text = req.responseText;
				var textObject = eval("(" + text + ")");
				var provinceNode = document.getElementById("provinces");
//				alert(textObject.length);
				
				for(var x = 0; x < textObject.length; x++) {
					var optionNode = document.createElement("option");
					var textNode = document.createTextNode(textObject[x].name);
					optionNode.appendChild(textNode);
					provinceNode.appendChild(optionNode);
				}
			}
		}
	}
	
	req.open("post","provinceServlet",true);
	req.send(null);
}

function getXMLHttpRequest() {
	var req;
	
	//根据不同的浏览器创建不同的XMLHttpRequest对象
	if(window.XMLHttpRequest) {  
        req = new XMLHttpRequest();  
    } else if(window.ActiveXObject) {  
        req = new ActiveXObject("Microsoft.XMLHTTP");  
    } 
	
	return req;
}

使用JSON-lib把集合或者对象转换成JSON格式字符串
第一步:添加如下jar包
  • json-lib-2.4-jdk15.jar
  • commons-lang-2.4.jar
  • commons-logging.jar
  • ezmorph-1.0.4.jar
  • commons-collections.jar
  • commons-beanutils.jar

第二步:测试
构建Province对象:
package com.hpu.model;

public class Province {
	private int id;
	private String name;

	public Province(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

编写测试类:
package com.hpu.gosn;

import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import com.hpu.model.Province;

public class GSON {
	public static void main(String[] args) {
		List<Province> lists = new ArrayList<Province>();
		Province pro1 = new Province(1,"吉林省");
		Province pro2 = new Province(2,"辽宁省");
		Province pro3 = new Province(3,"山东省");
		
		lists.add(pro1);
		lists.add(pro2);
		lists.add(pro3);
/*********************************************************************/
		//将一个集合转换成JSON格式
		JSONArray jsonArray = JSONArray.fromObject(lists);
		
		//将对象里面的所有属性全部转换成JSON形式
		//[{"id":1,"name":"吉林省"},{"id":2,"name":"辽宁省"},{"id":3,"name":"山东省"}]
		System.out.println(jsonArray.toString());
		
		//只要其中的一部分属性,去掉id
		//[{"name":"吉林省"},{"name":"辽宁省"},{"name":"山东省"}]
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[]{"id"});
		JSONArray jsonArray1 = JSONArray.fromObject(lists,jsonConfig);
		System.out.println(jsonArray1.toString());
/*********************************************************************/
		//将一个对象转换成JSON格式
		//{"id":1,"name":"吉林省"}
		JSONObject jsonObject = JSONObject.fromObject(pro1);
		System.out.println(jsonObject.toString());
		
		//只要其中的一部分属性,去掉id
		//{"name":"吉林省"}
		JsonConfig jsonConfigObject = new JsonConfig();
		jsonConfigObject.setExcludes(new String[]{"id"});
		JSONObject jsonObject2 = JSONObject.fromObject(pro1,jsonConfigObject);
		System.out.println(jsonObject2.toString());
	}
}