json数据转对象工具类

一、定义系统常量

Constant.java

<span style="font-size:18px;">public final static String RETURN_CODE="rtnCode";//返回编码
public final static String RETURN_MSG="rtnMsg";//返回信息
public static final String DATA = "data";//返回数据
public static final String PAGE_NO = "1";
public static final String PAGE_SIZE = "10";</span>

二、两种格式的数据

1、data不是数组的情况

<span style="font-size:18px;">{"rtnMsg":"成功","rtnCode":"00","data":{"id":"12","name":"jack","age":67}}</span>

2、data是数组的情况

<span style="font-size:18px;">{"rtnMsg":"成功","rtnCode":"00","data":[{"id":"12","name":"jack","age":67}]}</span>

三、json解析工具类

<span style="font-size:18px;">/**
	 * json转对象的实现
	 * @param jsonObj
	 * @param clazz
	 * @return
	 */
	public static <T> T getObjectFromJsonObject(JSONObject jsonObj,Class<T> clazz){
		T t = null ;
		try {
			t = JSON.parseObject(JSONObject.toJSONString(jsonObj), clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return t;
    }
	/**
	 * json转对象(data不是数组的情况)
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	public static <T> T getObjectFromJsonString(String jsonString,Class<T> clazz){
        JSONObject jsonObject = null;
        JSONObject info = null;
        T t = null;
        if (StringUtils.isBlank(jsonString)) {
			return null;
		}
        try {
            jsonObject = JSONObject.parseObject(jsonString);
            if (!Constant.SUCCEED.equals(jsonObject.get(Constant.RETURN_CODE))) {
				return null;
			}
            info = jsonObject.getJSONObject(Constant.DATA);
            t = getObjectFromJsonObject(info,clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
	/**
	 * json转对象(data是数组的情况)
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	public static List<?> getObjectListFromJsonString(String jsonString,Class<?> clazz){
        JSONObject jsonObject = null;
        JSONArray info = null;
        if (StringUtils.isBlank(jsonString)) {
			return null;
		}
        List list = new ArrayList();
        try {
            jsonObject = JSONObject.parseObject(jsonString);
            if (!Constant.SUCCEED.equals(jsonObject.get(Constant.RETURN_CODE))) {
				return null;
			}
            info = jsonObject.getJSONArray(Constant.DATA);
            for (int i = 0; i < info.size(); i++) {
				list.add(getObjectFromJsonObject(info.getJSONObject(i),clazz));
			}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
	
	/**
	 * json转对象(带分页的数据)
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	public static PageResult<?> getObjectListFromJsonStringWithPage(String jsonString,Class<?> clazz){
        JSONObject jsonObject = null;
        JSONArray info = null;
        Integer pageNo = null;
        Integer pageSize = null;
        Integer totalCount = null;
        if (StringUtils.isBlank(jsonString)) {
			return null;
		}
        List list = new ArrayList();
        try {
            jsonObject = JSONObject.parseObject(jsonString);
            if (!Constant.SUCCEED.equals(jsonObject.get(Constant.RETURN_CODE))) {
				return null;
			}
            pageNo = (Integer) jsonObject.get("pageNo");
            pageSize = (Integer) jsonObject.get("pageSize");
            if (pageSize==null) {
				pageSize = Integer.parseInt(Constant.PAGE_SIZE);
			}
            totalCount =  (Integer) jsonObject.get("totalCount");
            info = jsonObject.getJSONArray(Constant.DATA);
            for (int i = 0; i < info.size(); i++) {
				list.add(getObjectFromJsonObject(info.getJSONObject(i),clazz));
			}
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageResult pageResult = new PageResult();
        pageResult.setPageSize(pageSize);
        pageResult.setPageOffSet(pageNo);
        pageResult.setTotalCount(totalCount);
        pageResult.setResultList(list);
        return pageResult;
    }</span>

PS:PageResult请参看此篇博客http://blog.csdn.net/lsq_401/article/details/50668890


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值