一、定义系统常量
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.youkuaiyun.com/lsq_401/article/details/50668890