1. 程式人生 > >fastjson的簡單使用

fastjson的簡單使用

json資料格式:

{
    "data": [
        {
            "bp_id": "193934",
            "from_product": "193933",
            "name": "植薈",
            "cover": "xxx",
            "pic": "xxx",
            "big": "xxx",
            "content": "111111",
            "from_brand": "100",
            "price": "65.0"
        },
        {
            "bp_id": "193640",
            "from_product": "193639",
            "name": "華為xxx",
            "cover": "xxx",
            "pic": "xxx",
            "big": "xxx",
            "content": "",
            "from_brand": "109",
            "price": 705.6
        }
    ],
    "total_count": "97",
    "page_count": 49
}

1、簡單的FastJsonTools工具類
public class FastJsonTools {
	public FastJsonTools() {
		
	}
	
	/**
	 * 對單個javabean的解析
	 * @param jsonString
	 * @param cls
	 * @return
	 */
	public static <T> T getGoods(String jsonString, Class<T> cls) {
		T t = null;
		try {
			t = JSON.parseObject(jsonString, cls);	
		} catch (Exception e) {
			// TODO: handle exception
		}
		return t;
	}
	
	public static <T>  List<T> getGoods(String jsonStriung, Class<T> cls) {
		List<T> list = new ArrayList<T>();
		try {
			list = JSON.parseArray(jsonStriung, cls);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
	
	public static List<Map<String, Object>> listKeyMaps(String jsonString) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = JSON.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>(){});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
}

2、Goods.java
public class Goods{
	public List<Map<String, String>> data;//不常用的資料這裡我選擇了用list<Map<>>的形式
	public String total_count;
	public String page_count;
	/**
	 * @return the data
	 */
	public List<Map<String, String>> getData() {
		return data;
	}
	/**
	 * @param data the data to set
	 */
	public void setData(List<Map<String, String>> data) {
		this.data = data;
	}
	/**
	 * @return the total_count
	 */
	public String getTotal_count() {
		return total_count;
	}
	/**
	 * @param total_count the total_count to set
	 */
	public void setTotal_count(String total_count) {
		this.total_count = total_count;
	}
	/**
	 * @return the page_count
	 */
	public String getPage_count() {
		return page_count;
	}
	/**
	 * @param page_count the page_count to set
	 */
	public void setPage_count(String page_count) {
		this.page_count = page_count;
	}
}

上面的程式碼用物件的方式,可以這樣寫:
public class Goods{
	public List<GoodsInfo> data;
	public String total_count;
	public String page_count;
	/**
	 * @return the data
	 */
	public List<GoodsInfo> getData() {
		return data;
	}
	/**
	 * @param data the data to set
	 */
	public void setData(List<GoodsInfo> data) {
		this.data = data;
	}
	/**
	 * @return the total_count
	 */
	public String getTotal_count() {
		return total_count;
	}
	/**
	 * @param total_count the total_count to set
	 */
	public void setTotal_count(String total_count) {
		this.total_count = total_count;
	}
	/**
	 * @return the page_count
	 */
	public String getPage_count() {
		return page_count;
	}
	/**
	 * @param page_count the page_count to set
	 */
	public void setPage_count(String page_count) {
		this.page_count = page_count;
	}
}

3、GoodsInfo
public class Goods {
	public String bp_id;
	public String from_product;
	public String name;
	public String cover;
	public String pic;
	public String big;
	public String content;
	public String from_brand;
	public String price;
	
	/**
	 * @return the bp_id
	 */
	public String getBp_id() {
		return bp_id;
	}
	/**
	 * @param bp_id the bp_id to set
	 */
	public void setBp_id(String bp_id) {
		this.bp_id = bp_id;
	}
	/**
	 * @return the from_product
	 */
	public String getFrom_product() {
		return from_product;
	}
	/**
	 * @param from_product the from_product to set
	 */
	public void setFrom_product(String from_product) {
		this.from_product = from_product;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the cover
	 */
	public String getCover() {
		return cover;
	}
	/**
	 * @param cover the cover to set
	 */
	public void setCover(String cover) {
		this.cover = cover;
	}
	/**
	 * @return the pic
	 */
	public String getPic() {
		return pic;
	}
	/**
	 * @param pic the pic to set
	 */
	public void setPic(String pic) {
		this.pic = pic;
	}
	/**
	 * @return the big
	 */
	public String getBig() {
		return big;
	}
	/**
	 * @param big the big to set
	 */
	public void setBig(String big) {
		this.big = big;
	}
	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}
	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}
	/**
	 * @return the from_brand
	 */
	public String getFrom_brand() {
		return from_brand;
	}
	/**
	 * @param from_brand the from_brand to set
	 */
	public void setFrom_brand(String from_brand) {
		this.from_brand = from_brand;
	}
	/**
	 * @return the price
	 */
	public String getPrice() {
		return price;
	}
	/**
	 * @param price the price to set
	 */
	public void setPrice(String price) {
		this.price = price;
	}
	
}

還有種情況,如果類的成員變數和json的key值不相等,我們可以用以下方式進行序列化和反序列化

序列化(轉換成json時):

@JSONField(name="newJsonKey")
public String getName() {
return name;
}
反序列化時(json轉換成物件或javabean時):
@JSONField(name="newJsonKey")
public void setName(String name) {
this.name = name;
}

參考地址:http://my.eoe.cn/814017/archive/3374.html(博主的部落格還不錯哦)