加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

fastjson的简单使用

发布时间:2020-12-16 19:45:15 所属栏目:百科 来源:网络整理
导读:json数据格式: { "data": [ { "bp_id": "193934","from_product": "193933","name": "植荟","cover": "xxx","pic": "xxx","big": "xxx","content": "222221","from_brand": "100","price": "65.0" },{ "bp_id": "193640","from_product": "193639","name":

json数据格式:

{
    "data": [
        {
            "bp_id": "193934","from_product": "193933","name": "植荟","cover": "xxx","pic": "xxx","big": "xxx","content": "222221","from_brand": "100","price": "65.0"
        },{
            "bp_id": "193640","from_product": "193639","name": "华为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(博主的博客还不错哦)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读