JsonUtils.java
生活随笔
收集整理的這篇文章主要介紹了
JsonUtils.java
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
package com.mysteel.black.common.jsonutil;import java.util.List;import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper;/*** * 說明:ObjectMapper自定義響應結構* * @author 胥攀* @date 2016年6月17日*/ public class JsonUtils {// 定義jackson對象private static final ObjectMapper MAPPER = new ObjectMapper();/*** * 說明:將對象轉換成json字符串。* * @param 任意對象* @return* @author 胥攀* @time:2016年5月9日*/public static String objectToJson(Object data) {try {String string = MAPPER.writeValueAsString(data);return string;} catch (JsonProcessingException e) {e.printStackTrace();}return null;}/*** * 說明:將json結果集轉化為對象* * @param jsonData json數據, clazz 對象中的object類型* @return* @author 胥攀* @time:2016年5月9日*/public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {try {T t = MAPPER.readValue(jsonData, beanType);return t;} catch (Exception e) {e.printStackTrace();}return null;}/*** * 說明:將json數據轉換成pojo對象list* * @param jsonData* @param beanType* @return* @author 胥攀* @time:2016年5月9日*/public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);try {List<T> list = MAPPER.readValue(jsonData, javaType);return list;} catch (Exception e) {e.printStackTrace();}return null;}}?
轉載于:https://my.oschina.net/u/2253438/blog/728865
總結
以上是生活随笔為你收集整理的JsonUtils.java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何设置游戏分辨率(C++)
- 下一篇: Map字符串类型去掉空格处理