JSON转换工具(Gson)

xiaoxiao2021-02-28  82

package com.tdm.unicom.util; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.tdm.unicom.model.pointGift.PropertyPair; public class JSONUtils {     /**      * 对象转换成JSON格式的字符串      * @param obj      * @return      */     public static String object2String(Object obj){         GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();          return builder.create().toJson(obj);     }     /**      * 对象转换成JSON格式的字符串      * @param obj      * @return       * @return      */     public static <T> T stringToObject(String jsonString,Class<T> c){         GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();         return (T)builder.create().fromJson(jsonString, c);     }          /**      * 对象转换成JSON格式的字符串      * @param obj      * @return       * @return      */ //    public static <T>List<T> stringToAraay(String jsonString,Class<T> c){ //     Gson gson = new GsonBuilder() //        .excludeFieldsWithoutExposeAnnotation()  //不导出实体中没有用@Expose注解的属性   //        .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//时间转化为特定格式     //        //.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.   //        .setPrettyPrinting() //对json结果格式化.   //        .setVersion(1.0)    //有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.   //                            //@Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么   //                            //@Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.   //        .create();  // // List retList = gson.fromJson(jsonString,   //                new TypeToken<List<T>>(){}.getType());   //        return retList; //    }    /* public static void main(String[] args) { String s = "[{propertyKey:\"使用方法\",propertyValue:\"我不告诉你\"},{propertyKey:\"产品规格\",propertyValue:\"瞎弄的没用\"},{propertyKey:\"产地\",propertyValue:\"天上\"}]"; List<PropertyPair> listPropertyPair = JSONUtils.getGson().fromJson(s,                   new TypeToken<List<PropertyPair>>(){}.getType());   for(PropertyPair pp : listPropertyPair){ System.out.println("key:" + pp.getPropertyKey() + "  value:" + pp.getPropertyValue()); } }*/          public static Gson getGson(){     Gson gson = new GsonBuilder()         .excludeFieldsWithoutExposeAnnotation()  //不导出实体中没有用@Expose注解的属性           .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//时间转化为特定格式             //.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.           .setPrettyPrinting() //对json结果格式化.           .setVersion(1.0)    //有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.                               //@Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么                               //@Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.           .create();         return gson;     } }
转载请注明原文地址: https://www.6miu.com/read-39367.html

最新回复(0)