gson转换对象对字段别名处理@SerializedName注解demo

/**
 * 
 */
package json;

import java.util.Date;
import java.util.Objects;

import com.platformfee.util.tools.GsonUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;


public class GsonTest {
    
    public static TestResultVo handleTransTn(String json) {
        TestResultVo testResultVo = null;
        if (org.apache.commons.lang.StringUtils.isNotEmpty(json)) {
            Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateAdapter()).create();

            ResponseObject<TestResultVo> result = gson
                    .fromJson(
                            json,
                            new TypeToken<ResponseObject<TestResultVo>>() {
                            }.getType());
            if (Objects.nonNull(result) && Objects.equals(ErrorCode.SUCCESS.getCode(),result.getResCode())) {
                testResultVo = result.getData();
                System.out.println("handleTransTn result=[{}]"+GsonUtils.toJson(testResultVo));
            }
        }
        return testResultVo;
    }
    
    public static void main(String[] args) {
        String json = "{\"resCode\":\"111\",\"resMsg\":\"success\",\"data\":{\"package\":\"prepay_id=wx14163621135514d6fe36aa7854ce740000\"}}";
        TestResultVo vo = handleTransTn(json);
        System.out.println("vo="+GsonUtils.toJson(vo));
        
    }
}

 


/**
* */ package json; import com.google.gson.annotations.SerializedName; public class TestResultVo { String respMsg; // 响应编码 成功 00 String respCode; @SerializedName("package") String Package; //package是关键字 转换的时候需要处理首字母小写。 public String getRespMsg() { return respMsg; } public void setRespMsg(String respMsg) { this.respMsg = respMsg; } public String getRespCode() { return respCode; } public void setRespCode(String respCode) { this.respCode = respCode; } public String getPackage() { return Package; } public void setPackage(String package1) { Package = package1; } }
package json;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.util.Date;

public class DateAdapter implements JsonDeserializer <Date> {
    @Override
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
       Date date = new Date();
       date.setTime(json.getAsLong());
       return date;
    }
}
package json; 

public enum ErrorCode {
    SUCCESS("111","OK");
    
    
    private String code;
    private String text;
    
    private ErrorCode(String code, String text) {   
        this.code = code;   
        this.text = text;   
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) { 
        this.text = text;
    } 
    
}
 
/**
 * 
 */
package json; 


import java.io.Serializable;


public class ResponseObject<T> implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private T data;
    protected String resCode;
    protected String resMsg;
    
    public String getResCode() {
        return resCode;
    }
    public void setResCode(String resCode) {
        this.resCode = resCode;
    }
    public String getResMsg() {
        return resMsg;
    }
    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

 

posted on 2020-12-17 08:25  oktokeep  阅读(1956)  评论(0编辑  收藏  举报