JSON转换工具(Gson)

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值