JSON小结【json-lib】

javabean:Address

 

package com.baebae.model;

public class Address
{
	private String city;
	private String district;
	private String street;
	private int no;

	getter/setter()...
}

 

 

JAVA--JSON互相转换:

 

【1】JAVA——>JSON

 

/**
	 * <1> JAVA-->JSON
	 * 单个对象Object-->JSONObject
	 * 集合List/Array/Set----->JSONArray
	 */
	@Test
	public void testObj2JSON() throws InterruptedException {
		// 1.bean---->json
		Address addr = new Address();
		addr.setCity("北京");
		addr.setDistrict("清河");
		addr.setStreet("安宁庄东路");
		addr.setNo(200);
	
		JSONObject jsonObj = JSONObject.fromObject(addr);
		System.err.println("____________jsonObj_______________");
		System.out.println(jsonObj);
		// 1.集合-->JSONArray
		Set<Address> set = new HashSet<Address>();
		Address[] addrArray = new Address[100];
		List<Address> list = new ArrayList<Address>();
		for(int i=0;i<10;i++){
			Address address = new Address();
			address.setCity("北京");
			address.setDistrict("清河");
			address.setStreet("安宁庄东路"+i+"号");
			address.setNo(200+i);
			
			list.add(address);
			addrArray[i]=address;
			set.add(address);
		}
		// 这里不能用JSONObject.fromObject(collection)
		JSONArray jsonList = JSONArray.fromObject(list);
		JSONArray jsonArray = JSONArray.fromObject(addrArray);
		JSONArray jsonSet = JSONArray.fromObject(set);
		
		// 打印结果
		Thread.sleep(3000);
		System.err.println("____________jsonList_______________");
		System.out.println(jsonList);
		Thread.sleep(3000);
		System.err.println("_____________jsonArray______________");
		System.out.println(jsonArray);
		Thread.sleep(3000);
		System.err.println("______________jsonSet_____________");
		System.out.println(jsonSet);
		Thread.sleep(3000);
	}

打印结果:

 

 

____________jsonObj_______________
{"city":"北京","district":"清河","no":200,"street":"安宁庄东路"}
____________jsonList_______________
[{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"}]
_____________jsonArray______________
[{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]
______________jsonSet_____________
[{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"}]

  --------------------------------------------------------------------------------

 

【2】JSON---->JAVA

/**
	 * <2> JSON-->JAVA
	 * 1.如果字段名有错无法匹配,会发出警告;属性得到是null.
	 * 2.转换:	
	 *	->Object
	 * 		<1> 字符串封装成JSON 对象:【JSONObject.fromObject(jsonStr)】
	 * 		<2> JSON对象转换成Object:【(Address) JSONObject.toBean(jsonObject, Address.class)】
	 * 	->Collection
	 * 		<1> 字符串封装成JSONArray 对象:【JSONArray.fromObject(jsonArrayStr)】
	 * 		<2> JSONArray对象转换成Java集合:【(List<Address>)JSONArray.toCollection(jsonArray, Address.class)】
	 */
	@Test
	public void testJson2Java()  throws InterruptedException {
		// 1.JSON object
		String jsonStr = "{\"city\":\"北京\",\"district\":\"清河\",\"street\":\"安宁庄东路南口\",\"no\":500}";
		// 3.JSONArray
		String jsonArrayStr = "[{\"city\":\"北京\",\"district\":\"清河\",\"street\":\"安宁庄东路南口\",\"no\":500},{\"city\":\"北京\",\"district\":\"上地\",\"street\":\"安宁庄东路\",\"no\":300}]";
		
		JSONObject jsonObject = JSONObject.fromObject(jsonStr);
		JSONArray jsonArray = JSONArray.fromObject(jsonArrayStr);
		
		// JSONObject.toBean(jasonObject,Class): Bean;
		Address address  = (Address) JSONObject.toBean(jsonObject, Address.class);
		List<Address> list = (List<Address>)JSONArray.toCollection(jsonArray, Address.class);
		
		// 1.输出Object
		Thread.sleep(1000);
		System.out.println("District:"+address.getDistrict());
		// 2.输出Array
		Thread.sleep(1000);
		for(Address addr:list){
			System.out.println(addr.getCity()+"\t"+addr.getDistrict()+"\t"+addr.getStreet()+"\t"+addr.getNo());
		}
	}

 

打印结果:

District:清河
北京	清河	安宁庄东路南口	500
北京	上地	安宁庄东路	300

 

 

【总结】

JSON————>JAVA:

        ->Object                                                                                      

    <1> 字符串封装成JSON 对象:【JSONObject.fromObject(jsonStr)】                                        

    <2> JSON对象转换成Object:【(Address) JSONObject.toBean(jsonObject, Address.class)】              

        ->Collection                                                                                  

    <1> 字符串封装成JSONArray 对象:【JSONArray.fromObject(jsonArrayStr)】                               

    <2> JSONArray对象转换成Java集合:【(List<Address>)JSONArray.toCollection(jsonArray, Address.class)】

 

JAVA————>JSON:

        ObjectJSONObject.fromObject(addr)

      Collection:【JSONArray.fromObject(list)】

 

参考资料:

1.官方网站:http://sourceforge.net/projects/json-lib

2.所需jar包:

commons-beanutils-1.8.3.jar
commons-collections-3.2.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar

 

你可能感兴趣的:(json-lib)