【第3篇】通过JSON-Lib把数组转换成Json数据

package ivyy.taobao.com.domain.jsonlib;

import ivyy.taobao.com.entity.Address;

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

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

/**
 *@DEMO:json
 *@Java:ArrayToJson.java
 *@Date:2015-1-18下午5:17:19
 *@Author:liangjilong
 *@Email:[email protected]
 *@Weibo:http://weibo.com/jilongliang
 *@Version:1.0
 *@Description:通过JSON-Lib把数组转换成Json数据
 */
public class ArrayToJson {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		//getArrayToJson();
		getArrayToJsonByMap();
	}
	/**
	 * 通过ByJSONLib把数组转换成Json数据
	 * @param url
	 * @throws Exception
	 */
	private static void getArrayToJson()throws Exception
	{ 
		Address address=new Address();
		address.setProvince("广东省");
		address.setCity("云浮市");
		address.setDistrict("云城区东升布艺");
		address.setWeibo("http://weibo.com/resourceljl");
		JSONArray jsonArr = JSONArray.fromObject(address);  //把对象转换成Json对象
		List<Address> lists = JSONArray.toList(jsonArr, Address.class); //把对象Json数组转换成Json数组
	    for (Address obj : lists) {  
	    	System.out.println(obj.getProvince());
	    	System.out.println(obj.getCity());
	    	System.out.println(obj.getDistrict());
	    	System.out.println(obj.getWeibo());
	    }
	}
	/**
	 * 结合Map去通过JSON-Lib把数组转换成Json数据
	 * @param url
	 * @throws Exception
	 */
	private static void getArrayToJsonByMap()throws Exception
	{ 
		Address address=new Address();
		address.setProvince("广东省");
		address.setCity("云浮市");
		address.setDistrict("云城区东升布艺");
		address.setWeibo("http://weibo.com/resourceljl");
		JSONArray jsonArr = JSONArray.fromObject(address);  //把对象转换成Json对象
		 //注释掉的也可以执行
		Map<String, Class<Address>> map=new HashMap<String,Class<Address>>();
		map.put("address", Address.class);
	    List<Address> lists = JSONArray.toList(jsonArr, Address.class, map); //把对象Json数组转换成Json数组
	    
	    System.out.println("-----------------------方法一-----------------------");
	    //-----------------------方法一-----------------------
	    for (Address obj : lists) {  
	    	System.out.println(obj.getProvince());//获取省的数据
	    	System.out.println(obj.getCity());//获取城市的数据
	    	System.out.println(obj.getDistrict());//获取街道的数据
	    	System.out.println(obj.getWeibo());//获微博的数据
	    }
	    
	    System.out.println("-----------------------方法二-----------------------");
	    
	    //-----------------------方法二-----------------------
	    for (Iterator iterator = lists.iterator(); iterator.hasNext();) {
			Address obj = (Address) iterator.next();
			System.out.println(obj.getProvince());
	    	System.out.println(obj.getCity());
	    	System.out.println(obj.getDistrict());
	    	System.out.println(obj.getWeibo());
		}
	    
	    System.out.println("-----------------------方法三-----------------------");
	    //-----------------------方法三-----------------------
	    for (int i = 0; i < lists.size(); i++) {
	    	JSONObject obj = JSONObject.fromObject(lists.get(i));
	    	System.out.println(obj.get("province"));
	    	System.out.println(obj.get("city"));
	    	System.out.println(obj.get("district"));
	    	System.out.println(obj.get("weibo"));
	    	
		}
	    
	    System.out.println("-----------------------方法四-----------------------");
	    
		Iterator<Address> itera=lists.iterator();
		do{
			Address obj=itera.next();
			System.out.println(obj.getProvince());
	    	System.out.println(obj.getCity());
	    	System.out.println(obj.getDistrict());
	    	System.out.println(obj.getWeibo());
		}while(itera.hasNext());
		
		  
	    System.out.println("-----------------------方法五-----------------------");
		
		Iterator<Address> it = lists.iterator();
		
		//判断必须有值的时候就取值出来.如果没值的话会报错
		while (!it.hasNext()) {
			Address obj=itera.next();
			if(obj!=null)
			System.out.println(obj.getProvince());
	    	System.out.println(obj.getCity());
	    	System.out.println(obj.getDistrict());
	    	System.out.println(obj.getWeibo());
		}
	}
}
package ivyy.taobao.com.entity;

import java.io.Serializable;

import com.google.gson.annotations.Expose;
/**
 * Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
 * 创建Gson对象,没有@Expose注释的属性将不会被序列化
 * serialize序列化 默认 true
 * deserialize反序列化 默认 true
 * @author liangjilong
 * @Email:[email protected]
 */
public class Address implements Serializable{
	@Expose(serialize=false)// 序列化  /Address类已经序列化
	private int id;
	@Expose(deserialize=false)// 反序列化
	private String country;//国家
	private String province;//省份
	private String city;//城市
	private String street;//街道
	private String district;//地区
	private String cityCode;//邮政编码
	private String streetNumber;//街道号
	private String weibo;//微博
	public Address() {
	}
	
	public Address(int id,String country) {
		this.id=id;
		this.country=country;
	}
	public String getCityCode() {
		return cityCode;
	}
	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}
	public String getStreetNumber() {
		return streetNumber;
	}
	public void setStreetNumber(String streetNumber) {
		this.streetNumber = streetNumber;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getDistrict() {
		return district;
	}
	public void setDistrict(String district) {
		this.district = district;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}

	public int getId() {
		return id;
	}

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

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getWeibo() {
		return weibo;
	}

	public void setWeibo(String weibo) {
		this.weibo = weibo;
	}
	
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。