JSONObject测试

JSON : http://www.json.org/json-zh.html

JSONObject简介 :http://www.cnblogs.com/java-pan/archive/2012/04/07/JSONObject.html

1 Student.java

package com.test.domain;

public class Student {
	private int intValue;
	private double doubleValue;
	private long longValue0;
	private boolean booleanValue0;
	private Boolean booleanValue1;
	private String strintValue;
	private java.util.Date utilDateValue;
	private java.sql.Date sqlDateValue;
	private Integer IntegerValue;
	private Long LongValue1;
	private Teacher teacher;
	
	//getter/setter
	public int getIntValue() {
		return intValue;
	}
	public void setIntValue(int intValue) {
		this.intValue = intValue;
	}
	public double getDoubleValue() {
		return doubleValue;
	}
	public void setDoubleValue(double doubleValue) {
		this.doubleValue = doubleValue;
	}
	public String getStrintValue() {
		return strintValue;
	}
	public void setStrintValue(String strintValue) {
		this.strintValue = strintValue;
	}
	public java.util.Date getUtilDateValue() {
		return utilDateValue;
	}
	public void setUtilDateValue(java.util.Date utilDateValue) {
		this.utilDateValue = utilDateValue;
	}
	public java.sql.Date getSqlDateValue() {
		return sqlDateValue;
	}
	public void setSqlDateValue(java.sql.Date sqlDateValue) {
		this.sqlDateValue = sqlDateValue;
	}
	public Integer getIntegerValue() {
		return IntegerValue;
	}
	public void setIntegerValue(Integer integerValue) {
		IntegerValue = integerValue;
	}
	public long getLongValue0() {
		return longValue0;
	}
	public void setLongValue0(long longValue0) {
		this.longValue0 = longValue0;
	}
	public Long getLongValue1() {
		return LongValue1;
	}
	public void setLongValue1(Long longValue1) {
		LongValue1 = longValue1;
	}
	public boolean isBooleanValue0() {
		return booleanValue0;
	}
	public void setBooleanValue0(boolean booleanValue0) {
		this.booleanValue0 = booleanValue0;
	}
	public Boolean getBooleanValue1() {
		return booleanValue1;
	}
	public void setBooleanValue1(Boolean booleanValue1) {
		this.booleanValue1 = booleanValue1;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
}

2.testJsonObject.java 测试client类

package com.test.Json.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.test.domain.Student;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/** 
 * @author z10
 * @version 创建时间:Dec 6, 2013
 */
public class testJsonObject {

	public static void main(String[] args) {
		//1. List集合转换成json代码
		List list = new ArrayList();
		list.add("first");
		list.add("second");
		list.add("third");
		JSONArray jsonArray2 = JSONArray.fromObject(list);
		System.out.println("1 List集合转换成json " + jsonArray2.toString());
		//["first","second","third"]

		//2. Map集合转换成json代码
		Map map = new HashMap();
		map.put("name", "json");
		map.put("bool", Boolean.TRUE);
		map.put("int", new Integer(1));
		map.put("arr", new String[] { "a", "b" });
		map.put("func", "function(i){ return this.arr[i]; }");
		JSONObject json = JSONObject.fromObject(map);
		System.out.println("2 Map集合转换成json " + json.toString());
		/*打印后格式化为
		{
			"arr":["a","b"],
			"int":1,
			"name":"json",
			"func":function(i){ return this.arr[i]; },
			"bool":true
		}
			 * */
		//3. Bean转换成json代码
		JSONObject jsonObject = JSONObject.fromObject(new Student());
		System.out.println("3 Bean转换成json " + jsonObject.toString());
/*打印内容手动格式化后:
 {
	"booleanValue0":false,
     "booleanValue1":false,
	 "doubleValue":0,
	 "intValue":0,
	 "integerValue":0,
	 "longValue0":0,
	 "longValue1":0,
	 "sqlDateValue":null,
	 "strintValue":"",
	 "teacher":null,
	 "utilDateValue":null
}
 */
		//4. 数组转换成json代码
		boolean[] boolArray = new boolean[] { true, false, true };
		JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
		System.out.println("4 数组转换成json " + jsonArray1.toString());
         // [true,false,true]
		
		//5. 一般数据转换成json代码
		JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
		System.out.println("5一般数据转换成json " + jsonArray3.toString());
		// ["json","is","easy"]
	}
}

相关推荐