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


    /**
     * 
     */
    package json;
    
    import java.util.Date;
    import java.util.Objects;
    
    import com.autoyol.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;
        }
    
    }
  • 相关阅读:
    模板实参推断
    Koa2介绍及环境搭建
    nodejs之http.request
    nodejs请求json数据
    nodejs之url模块
    Nodejs之querystring 查询字符串
    将导航条设置成透明的
    10.12.1 安装cocoapods及使用详解
    UIViewAnimation动画
    iPhone屏幕尺寸、分辨率及适配
  • 原文地址:https://www.cnblogs.com/oktokeep/p/14147722.html
Copyright © 2020-2023  润新知