美文网首页android开发
Gson和fastJson 解析嵌套泛型的json数据

Gson和fastJson 解析嵌套泛型的json数据

作者: 你好好吖 | 来源:发表于2018-11-15 14:49 被阅读14次
            /*
             * {
             "code": 1,
             "datas": [{
             "address": "深圳",
             "age": 18,
             "email": "123@qq.com",
             "nickName": "小明",
             "phoneNumber": "11111111111",
             "sex": "男"
             }, {
             "address": "广州",
             "age": 21,
             "email": "234@qq.com",
             "nickName": "小红",
             "phoneNumber": "22222222222",
             "sex": "女"
             }, {
             "address": "北京",
             "age": 19,
             "email": "453@qq.com",
             "nickName": "小黑",
             "phoneNumber": "33333333333",
             "sex": "男"
             }, {
             "address": "杭州",
             "age": 18,
             "email": "789@qq.com",
             "nickName": "小强",
             "phoneNumber": "44444444444",
             "sex": "男"
             }],
             "msg": "成功"
             }
             */
            System.out.print(str);
     
            
            /*
            fastJson 方式
             */
            ResponseResult<List<UserInfo>> responseResult1 = JSON.parseObject(str, new TypeReference<ResponseResult<List<UserInfo>>>() {});
            System.out.print(responseResult1);
     
            /*
            Gson方式
             */
            Gson gson = new Gson();
            ResponseResult<List<UserInfo>> responseResult2 = gson.fromJson(str , new TypeToken<ResponseResult<List<UserInfo>>>(){}.getType());
            System.out.print(responseResult2);
    

    包装泛型实体类

    public class ResponseResult<T> {
        /**
         * 服务器返回标识
         */
        private int code;
     
        /**
         * 描述
         */
        private String msg;
     
        /**
         * 请求成功之后的数据
         */
        private T datas;
     
        public int getCode() {
            return code;
        }
     
        public void setCode(int code) {
            this.code = code;
        }
     
        public String getMsg() {
            return msg;
        }
     
        public void setMsg(String msg) {
            this.msg = msg;
        }
     
        public T getDatas() {
            return datas;
        }
     
        public void setDatas(T datas) {
            this.datas = datas;
        }
    }
    

    实体类

    public class UserInfo {
     
        private String nickName;
     
        private Integer age;
     
        private String sex;
     
        private String address;
     
        private String email;
     
        private String phoneNumber;
     
        public String getNickName() {
            return nickName;
        }
     
        public void setNickName(String nickName) {
            this.nickName = nickName;
        }
     
        public Integer getAge() {
            return age;
        }
     
        public void setAge(Integer age) {
            this.age = age;
        }
     
        public String getSex() {
            return sex;
        }
     
        public void setSex(String sex) {
            this.sex = sex;
        }
     
        public String getAddress() {
            return address;
        }
     
        public void setAddress(String address) {
            this.address = address;
        }
     
        public String getEmail() {
            return email;
        }
     
        public void setEmail(String email) {
            this.email = email;
        }
     
        public String getPhoneNumber() {
            return phoneNumber;
        }
     
        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
    }
    

    相关文章

      网友评论

        本文标题:Gson和fastJson 解析嵌套泛型的json数据

        本文链接:https://www.haomeiwen.com/subject/gvsyfqtx.html