1. 程式人生 > >解析json之net.sf.json

解析json之net.sf.json

下載地址

本次使用版本:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/
最新版本:http://sourceforge.net/projects/json-lib/files/json-lib/

使用net.sf.json需要匯入的包

JSONObject

package com.itlwc.test;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test {
	public static void main(String[] args) {
		// 建立JSONObject
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("username", "lwc");
		jsonObject.put("password", "123");
		// 列印:1
		System.out.println(jsonObject);

		// 增加屬性,列印:2
		jsonObject.element("sex", "男");
		System.out.println(jsonObject);
		
		// 根據key返回,列印:3
		System.out.println(jsonObject.get("sex"));

		// 判讀輸出物件的型別
		boolean isArray = jsonObject.isArray();
		boolean isEmpty = jsonObject.isEmpty();
		boolean isNullObject = jsonObject.isNullObject();
		// 列印:4
		System.out.println("是否陣列:" + isArray + " 是否空:" + isEmpty + " 是否空物件:"
				+ isNullObject);
		
		// 把JSONArray增加到JSONObject中
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(0, "lwc");
		jsonArray.add(1, "nxj");
		// 開始增加
		jsonObject.element("student", jsonArray);
		// 列印:5
		System.out.println(jsonObject);
	}
}
/*
列印結果
	{"username":"lwc","password":"123"}
	{"username":"lwc","password":"123","sex":"男"}
	男
	是否為陣列:false 是否為空:false 是否為空物件:false
	{"username":"lwc","password":"123","sex":"男","student":["lwc","nxj"]}
*/

JSONArray

package com.itlwc.test;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test {
	public static void main(String[] args) {
		//建立JSONArray
		JSONArray jsonArray = new JSONArray();
        jsonArray.add(0, "lwc");   
        jsonArray.add(1, "nxj");
        jsonArray.element("mxj");
        //列印:1
        System.out.println(jsonArray);
        
        //根據下標返回,列印:2
        System.out.println(jsonArray.get(0));
        
        //根據下標設定新值,列印:3
        jsonArray.set(0, "itlwc");
        System.out.println(jsonArray);
        
        //把JSONObject放入到JSONArray中
        JSONObject jsonObject = new JSONObject();
		jsonObject.put("username", "lwc");
		jsonObject.put("password", "123");
		//開始增加,列印:4
		jsonArray.add(jsonObject);
		System.out.println(jsonArray);
		
		//遍歷,列印:5
		for(int i=0;i<jsonArray.size();i++){
			System.out.print(jsonArray.get(i)+"\t");
		}
	}
}
/*
列印結果
	["lwc","nxj","mxj"]
	lwc
	["itlwc","nxj","mxj"]
	["itlwc","nxj","mxj",{"username":"lwc","password":"123"}]
	itlwc	nxj	mxj	{"username":"lwc","password":"123"}
*/

JavaBean與json字串互轉

package com.itlwc.test;

import net.sf.json.JSONObject;

import com.itlwc.entity.Student;

public class Test {
	public static void main(String[] args) {
		// JavaBean物件轉json字串
		Student stu1 = new Student("lwc", "111111");
		JSONObject jsonObject = JSONObject.fromObject(stu1);
		System.out.println(jsonObject);

		// json字串轉JavaBean
		String jsondata = "{\"password\":\"111111\",\"username\":\"lwc\"}";
		JSONObject jsonObject1 = JSONObject.fromObject(jsondata);
		Student stu2 = (Student) JSONObject.toBean(jsonObject1, Student.class);
		System.out.println(stu2);
	}
}
/*
列印結果:
	{"password":"111111","username":"lwc"}
	使用者: lwc 密碼:111111
*/

List與json字串互轉

package com.itlwc.test;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.itlwc.entity.Student;

public class Test {
	public static void main(String[] args) {
		// List轉json字串
		List list = new ArrayList();
		list.add(new Student("lwc", "111111"));
		list.add(new Student("nxj", "222222"));
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray);

		// json字串轉List
		List list1 = new ArrayList();
		String jsondata = "[{\"password\":\"111111\",\"username\":\"lwc\"},{\"password\":\"222222\",\"username\":\"nxj\"}]";
		JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
		for (int i = 0; i < jsonArray1.size(); i++) {
			JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
			Student stu2 = (Student) JSONObject.toBean(jsonObject2,
					Student.class);
			list1.add(stu2);
		}
		System.out.println(list1);
	}
}
/*
列印結果:
	[{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}]
	[使用者: lwc 密碼:111111, 使用者: nxj 密碼:222222]
*/

Map與json字串互轉

package com.itlwc.test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;

import com.itlwc.entity.Student;

public class Test {
	public static void main(String[] args) {
		// Map轉json字串
		Map map = new HashMap();
		map.put("1", new Student("lwc", "111111"));
		map.put("2", new Student("nxj", "222222"));
		JSONObject jsonMap = JSONObject.fromObject(map);
		System.out.println(jsonMap);

		// json字串轉Map
		String jsondata = "{\"2\":{\"password\":\"222222\",\"username\":\"nxj\"},\"1\":{\"password\":\"111111\",\"username\":\"lwc\"}}";
		Map map1 = (Map) JSONObject.fromObject(jsondata);
		Set set = map1.keySet();
		Iterator ite = set.iterator();
		while (ite.hasNext()) {
			String key = (String) ite.next();
			JSONObject jsonObject = JSONObject.fromObject(map1.get(key));
			Student stu = (Student) JSONObject
					.toBean(jsonObject, Student.class);
			System.out.println(key + " " + stu);
		}

	}
}
/*
列印結果: 
	{"2":{"password":"222222","username":"nxj"},"1":{"password":"111111","username":"lwc"}}
	2 使用者: nxj 密碼:222222
	1 使用者: lwc 密碼:111111
*/

JSONArray與List互轉

package com.itlwc.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import com.itlwc.entity.Student;

public class Test {
	public static void main(String[] args) {
		//List轉型JSONArray
		List<Student> list = new ArrayList<Student>();  
		list.add(new Student("lwc", "111111"));
		list.add(new Student("nxj", "222222")); 
        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray.toString());
        
        //JSONArray轉型List
		List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
		Iterator<Student> ite =  list2.iterator();
		while(ite.hasNext()){
			Student stu =ite.next();
			System.out.println(stu);
		}
	}
}
/*
列印結果
	[{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}]
	使用者: lwc 密碼:111111
	使用者: nxj 密碼:222222
*/

JSONArray與陣列互轉

package com.itlwc.test;

import net.sf.json.JSONArray;

public class Test {
	public static void main(String[] args) {
		// Java陣列轉JSONArray
		boolean[] boolArray = new boolean[] { true, false, true };
		JSONArray jsonArray = JSONArray.fromObject(boolArray);
		System.out.println(jsonArray.toString());

		// JSONArray轉Java陣列
		Object obj[] = jsonArray.toArray();
		for (Object o : obj) {
			System.out.print(o + " ");
		}
	}
}
/*
列印結果 :
	[true,false,true]
	true false true 
*/

XML與JSON互轉

需要匯入xom-1.1.jar
package com.itlwc.test;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

public class Test {
	public static void main(String[] args) throws Exception {
		// XML轉JSON
		String xml = "<root>" + "<name type='type'>zhaipuhong</name>"
				+ "<gender>male</gender>" + "<birthday>" + "<year>1970</year>"
				+ "<month>12</month>" + "<day>17</day>" + "</birthday>"
				+ "</root>";
		XMLSerializer xmlSerializer = new XMLSerializer();
		JSON json = xmlSerializer.read(xml);
		System.out.println(json.toString(2));

		// JSON轉XML
		String jsondata = "{\"root\":{" + "\"name\":\"zhaipuhong\","
				+ "\"gender\":\"male\"," + "\"birthday\":{"
				+ "\"year\":\"1970\"," + "\"month\":\"12\"," + "\"day\":\"17\""
				+ "}" + "}" + "}";
		JSONObject jsonObject = JSONObject.fromObject(jsondata);
		String xmlstr = new XMLSerializer().write(jsonObject);
		System.out.println(xmlstr);
	}
}
/*
列印結果:
	{
	  "name": "zhaipuhong",
	  "gender": "male",
	  "birthday":   {
	    "year": "1970",
	    "month": "12",
	    "day": "17"
	  }
	}
	<?xml version="1.0" encoding="UTF-8"?>
	<o>
		<root class="object">
			<birthday class="object">
				<day type="string">17</day>
				<month type="string">12</month>
				<year type="string">1970</year>
			</birthday>
			<gender type="string">male</gender>
			<name type="string">zhaipuhong</name>
		</root>
	</o>
*/