jackson 完美用法

来源:互联网 发布:知乎机构号注册流程 编辑:程序博客网 时间:2024/06/05 20:24
  1. import java.io.StringWriter;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.codehaus.jackson.JsonFactory;  
  8. import org.codehaus.jackson.JsonGenerator;  
  9. import org.codehaus.jackson.JsonParser;  
  10. import org.codehaus.jackson.map.DeserializationConfig.Feature;  
  11. import org.codehaus.jackson.map.MappingJsonFactory;  
  12. import org.codehaus.jackson.map.ObjectMapper;  
  13. import org.codehaus.jackson.type.JavaType;  
  14.   
  15. public class JsonUtils {  
  16.   
  17.     public static <K, V> String toJson(Map<K, V> obj) {  
  18.         String jsonString = "";  
  19.         try {  
  20.             if (obj == null) {  
  21.                 return "";  
  22.             }  
  23.             StringWriter sw = new StringWriter();  
  24.             JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);  
  25.             ObjectMapper mapper = new ObjectMapper();  
  26.             mapper.writeValue(gen, obj);  
  27.             jsonString = sw.toString();  
  28.             sw.close();  
  29.         } catch (Exception ex) {  
  30.             ex.printStackTrace();  
  31.             return "";  
  32.         }  
  33.   
  34.         return jsonString;  
  35.     }  
  36.   
  37.     @SuppressWarnings("unchecked")  
  38.     public static Map<String, Object> toMap(String json) {  
  39.         Map<String, Object> result = new HashMap<String, Object>();  
  40.         try {  
  41.             if (json == null || json.equals("")) {  
  42.                 return null;  
  43.             }  
  44.   
  45.             ObjectMapper objectMapper = new ObjectMapper();  
  46.             result = objectMapper.readValue(json, Map.class);  
  47.             if (result == null) {  
  48.                 return new HashMap<String, Object>();  
  49.             }  
  50.         } catch (Exception ex) {  
  51.             ex.printStackTrace();  
  52.         }  
  53.   
  54.         return result;  
  55.     }  
  56.   
  57.     public static String toJson(Object object) {  
  58.         String json = "";  
  59.         try {  
  60.             if (object == null) {  
  61.                 return "";  
  62.             }  
  63.             StringWriter sw = new StringWriter();  
  64.             JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);  
  65.             ObjectMapper mapper = new ObjectMapper();  
  66.   
  67.             mapper.writeValue(gen, object);  
  68.   
  69.             json = sw.toString();  
  70.             sw.close();  
  71.         } catch (Exception ex) {  
  72.             ex.printStackTrace();  
  73.             json = "";  
  74.         }  
  75.         return json;  
  76.     }  
  77.   
  78.     public static <T> T toObject(String json, Class<T> clazz) {  
  79.         T obj = null;  
  80.         try {  
  81.             if (json == null || json.equals("")) {  
  82.                 return null;  
  83.             }  
  84.             JsonFactory jsonFactory = new MappingJsonFactory();  
  85.             JsonParser jsonParser = jsonFactory.createJsonParser(json);  
  86.             ObjectMapper mapper = new ObjectMapper();  
  87.             mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
  88.             obj = mapper.readValue(jsonParser, clazz);  
  89.   
  90.         } catch (Exception ex) {  
  91.             ex.printStackTrace();  
  92.             obj = null;  
  93.         }  
  94.         return obj;  
  95.   
  96.     }  
  97.   
  98.     public static <T> List<T> toList(String json, Class<T> clazz) {  
  99.         if (json == null || json.equals("")) {  
  100.             return null;  
  101.         }  
  102.         ObjectMapper mapper = new ObjectMapper();  
  103.         mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
  104.         List<T> list = new ArrayList<T>();  
  105.         try {  
  106.             JavaType type = mapper.getTypeFactory().constructParametricType(List.class, clazz);  
  107.             list = mapper.readValue(json, type);  
  108.             return list;  
  109.         } catch (Exception e) {  
  110.             e.printStackTrace();  
  111.         }  
  112.   
  113.         return list;  
  114.     }  
  115. }  
原创粉丝点击