jackson 之 json 转 JavaBean、List 等
jackson 将 json 字符串解析成 List、JavaBean、BtPageResp 对象
获取List
public static <T> List<T> getListFromReturnValue(String json, Class<T> cls) {
try {
JsonNode jsonNode = objectMapper.readTree(json);//处理json中有多个对象的方法
JsonNode resultValue = jsonNode.findValue("content");
JavaType javaType = getCollectionType(ArrayList.class, cls);
List<T> lst = (List<T>)objectMapper.readValue(resultValue.toString(), javaType);
return lst;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
法二:
public static <T> List<T> getListFromInterface(String json, Class<T> cls) {
try {
JsonNode jsonNode = objectMapper.readTree(json);//处理json中有多个对象的方法
JavaType javaType = getCollectionType(ArrayList.class, cls);
return objectMapper.readValue(jsonNode.toString(), javaType);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
调用:
List<S_S_B_0013Req> list = BaseJsonUtil.getListFromInterface(BaseJsonUtil.encode(tm.getData()), S_S_B_0013Req.class);
获取JavaBean
/**
* 返回javabean对象
* @param json
* @param cls
* @param <T>
* @return
*/
public static <T> T getJavaBean(String json, Class<T> cls) {
try {
JsonNode jsonNode = objectMapper.readTree(json);//处理json中有多个对象的方法
JsonNode resultValue = jsonNode.findValue("content");
T t = objectMapper.readValue(resultValue.toString(), cls);
return t;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
获取 BtPageResp 分页对象
/**
* 返回分页对象
* @param json
* @param cls
* @param <T>
* @return
*/
public static <T> BtPageResp<T> getBtPageResp(String json, Class<T> cls) {
try {
Page<T> page = getJavaBean(json, Page.class);
return new BtPageResp<T>(page);
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
获取Object
/**
* 将传入进来的对象,将返回值content的值为需要的实体类
* @param json
* @param cls
* @param <T>
* @return
*/
public static <T> AjaxMessage<T> getObject(String json, Class<T> cls) {
try {
AjaxMessage msg = objectMapper.readValue(json, new TypeReference<AjaxMessage>(){});
T k = objectMapper.convertValue(msg.getContent(), cls);
return new AjaxMessage.Builder<T>(new DefaultCode(msg.getCode(),msg.getMessage())).content(k).build();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
如何调用
使用 getObject 直接获取最终的对象,支持多级泛型的解析
AjaxMessage<Page<List<Department>>> result = JsonUtils.getObject(resultStr, new TypeReference<AjaxMessage<Page<List<Department>>>>() {});
AjaxMessage<List<MenuNode>> result = JsonUtils.getObject(dataStr, new TypeReference<AjaxMessage<List<MenuNode>>>() {});
将对象转换成json字符串
/**
* encode 将传入进来的对象,转换成json字符串
*
* @param obj
* @return
*/
public static String encode(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
注意: 对象中有 List 不适用。
jackson出现错误 Unrecognized field,几种处理方法
解决方法:
1、请求的JSON里面字段多余映射的实体类,可以通过在类的顶部添加注解:
@JsonIgnoreProperties
2.0版本引入
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
参考:
https://blog.csdn.net/Dracotianlong/article/details/50506079
https://www.cnblogs.com/quanyongan/archive/2013/04/16/3024993.html
Jackson 框架的高阶应用:
https://www.ibm.com/developerworks/cn/java/jackson-advanced-application/index.html
所有发生在我们身上的事件都是一个经过仔细包装的礼物。只要我们愿意面对它有时候有点丑恶的包装,带着耐心和勇气一点一点的拆开包装的话,我们会惊喜的看到里面珍藏的礼物。
----遇见未知的自己