`
school104
  • 浏览: 72945 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
json JSON小结【json-lib】
package com.baebae.model;

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

	getter/setter()...
}
json JSON小结【json-lib】
/**
	 * <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);
	}
json JSON小结【json-lib】
/**
	 * <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());
		}
	}
Global site tag (gtag.js) - Google Analytics