json字符串与相应的JavaBean对象的相互转换

  1. import java.io.IOException;  
  2.   
  3. import com.fasterxml.jackson.core.JsonParseException;  
  4. import com.fasterxml.jackson.core.JsonProcessingException;  
  5. import com.fasterxml.jackson.databind.JavaType;  
  6. import com.fasterxml.jackson.databind.JsonMappingException;  
  7. import com.fasterxml.jackson.databind.ObjectMapper;  
  8.   
  9.   
  10. public class JacksonUtil {  
  11.     static ObjectMapper objectMapper;  
  12.   
  13.   
  14.     /** 
  15.      * 使用泛型方法,把json字符串转换为相应的JavaBean对象。 转换为普通JavaBean:readValue(json,Student.class) 
  16.      * 转换为List:readValue(json,List.class ).但是如果我们想把json转换为特定类型的List,比如List 
  17.      * <Student>,就不能直接进行转换了。 因为readValue(json,List .class)返回其实是List 
  18.      * <Map>类型,你不能指定readValue()的第二个参数是List<Student >.class,所以不能直接转换。 
  19.      * 我们可以把readValue()的第二个参数传递为Student[].class.然后使用Arrays.asList ();方法把得到的数组转换为特定类型的List。 
  20.      * 转换为Map:readValue(json,Map.class) 我们使用泛型,得到的也是泛型 
  21.      *  
  22.      * @param content 要转换的JavaBean类型 
  23.      * @param valueType 原始json字符串数据 
  24.      * @return JavaBean对象 
  25.      * @throws IOException 
  26.      * @throws JsonMappingException 
  27.      * @throws JsonParseException 
  28.      */  
  29.     public static <T> T readValue(String content, Class<T> valueType)  
  30.             throws JsonParseException, JsonMappingException, IOException {  
  31.         if (objectMapper == null) {  
  32.             objectMapper = new ObjectMapper();  
  33.         }  
  34.         return objectMapper.readValue(content, valueType);  
  35.     }  
  36.   
  37.   
  38.     public static <T> T readValue(String content, JavaType javaType)  
  39.             throws JsonParseException, JsonMappingException, IOException {  
  40.         if (objectMapper == null) {  
  41.             objectMapper = new ObjectMapper();  
  42.         }  
  43.         return objectMapper.readValue(content, javaType);  
  44.     }  
  45.   
  46.   
  47.     /** 
  48.      * 把JavaBean转换为json字符串 普通对象转换:toJson(Student) List转换:toJson(List) Map转换:toJson(Map) 
  49.      * 我们发现不管什么类型,都可以直接传入这个方法 
  50.      *  
  51.      * @param object JavaBean对象 
  52.      * @return json字符串 
  53.      * @throws JsonProcessingException 
  54.      */  
  55.     public static String toJson(Object object) throws JsonProcessingException {  
  56.         if (objectMapper == null) {  
  57.             objectMapper = new ObjectMapper();  
  58.         }  
  59.         return objectMapper.writeValueAsString(object);  
  60.     }  
  61.   
  62.   
  63.     public static JavaType getCollectionType(Class<?> collectionClass,  
  64.             Class<?>... elementClasses) {  
  65.         if (objectMapper == null) {  
  66.             objectMapper = new ObjectMapper();  
  67.         }  
  68.         return objectMapper.getTypeFactory().constructParametricType(collectionClass,  
  69.                 elementClasses);  
  70.     }  
  71.   
  72.   
  73.     /** 
  74.      * 私有的构造函数 
  75.      */  
  76.     private JacksonUtil() {  
  77.   
  78.   
  79.     }  
  80. }  
  81.    /**将json字符串数组转为List集合中的对象
        * 
        * 
        */
       public static <T> List<T> jsonToList(String json, Class<T> className) {
         

        JSONArray jsonArray = JSONArray.fromObject(json);

        List<T> ts  = jsonArray.toList(jsonArray, className);
         
     return ts;


       }
  82. /**
    * 新加:json字符串部分属性转javaBean 


    */
       public static <T> T jsonToBean(String jsonString, Class<T> beanCalss) {
            
           JSONObject jsonObject = JSONObject.parseObject(jsonString);
           
           T bean = (T) JSONObject.toJavaObject(jsonObject, beanCalss);
           
           return bean;
            
       }


猜你喜欢

转载自blog.csdn.net/qq_33223299/article/details/80421268