Gosn解析json数据(对象套对象)

一、Json数据

{
    "code": 1000,
    "data": {
        "openId": "oHA70CYET0",
        "phoneStatus": 23541252,
        "token": "1642920E766737E",
        "uid": "n4kCXnRPdWF8++LLsZH04810EYRa",
        "updated": false,
        "userInfo": {
            "avatar":"pibWVVtQyIPw9qiXzSQDS0icrB03F59GXb5yVhsL",
            "birthday": "1900-25-48",
            "gender": 0155,
            "nickname": "jsonjsonm",
            "phone": "156850478952634892145",
            "username": "233333"
        }
    },
    "msg": "请求成功"
}
//以这个数据为例

二、解析Json数据

1.创建bean类

public class UserBean {
    private Number code;
    private String msg;
    private DataBean data;//重点部分

    public Number getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public DataBean getData() {
        return data;
    }

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

    @Override
    public String toString() {
        return "UserBean{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public static class DataBean {
        private String openId;
        private String uid;
        private String token;
        private Boolean phoneStatus;
        private Boolean updated;
        public UserInfoBean userInfo;//重点部分

        public String getOpenId() {
            return openId;
        }

        public void setOpenId(String openId) {
            this.openId = openId;
        }

        public String getUid() {
            return uid;
        }

        public void setUid(String uid) {
            this.uid = uid;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public Boolean getPhoneStatus() {
            return phoneStatus;
        }

        public void setPhoneStatus(Boolean phoneStatus) {
            this.phoneStatus = phoneStatus;
        }

        public Boolean getUpdated() {
            return updated;
        }

        public void setUpdated(Boolean updated) {
            this.updated = updated;
        }

        public UserInfoBean getUserInfo() {
            return userInfo;
        }

        public void setUserInfo(UserInfoBean userInfo) {
            this.userInfo = userInfo;
        }

        @Override
        public String toString() {
            return "DataBean{" +
                    "openId='" + openId + '\'' +
                    ", uid='" + uid + '\'' +
                    ", token='" + token + '\'' +
                    ", phoneStatus=" + phoneStatus +
                    ", updated=" + updated +
                    ", userInfo=" + userInfo +
                    '}';
        }
    }

    
    public static class UserInfoBean {
        private String nickname;
        private String avatar;
        private Number gender;
        private String phone;
        private String birthday;
        private String uid;
        private String userName;

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }

        public Number getGender() {
            return gender;
        }

        public void setGender(Number gender) {
            this.gender = gender;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getBirthday() {
            return birthday;
        }

        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }

        public String getUid() {
            return uid;
        }

        public void setUid(String uid) {
            this.uid = uid;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        @Override
        public String toString() {
            return "UserInfoBean{" +
                    "nickname='" + nickname + '\'' +
                    ", avatar='" + avatar + '\'' +
                    ", gender=" + gender +
                    ", phone='" + phone + '\'' +
                    ", birthday='" + birthday + '\'' +
                    ", uid='" + uid + '\'' +
                    ", userName='" + userName + '\'' +
                    '}';
        }
    }
}

2.获取JSON数据

首先要有Gson依赖

 //Gson
    implementation 'com.google.code.gson:gson:2.6.2'

Json数据解析

Gson gson = new Gson();
//json即上面的json数据
UserBean userBean=gson.fromJson(json,UserBean.class);
//举例:获取第一层的msg数据,使用Log打印出来
Log.e("TGA","第一层的msg数据:"+userBean.getMsg());
//举例:获取第二层的openId数据,使用Log打印出来
UserBean.DataBean dataBean=userBean.getData();
Log.e("TGA","第二层的openId数据:"+dataBean.getOpenId());
//举例:获取第三层的nickname数据、phone数据,使用Log打印出来
UserBean.UserInfoBean userInfoBean=dataBean.getUserInfo();
Log.e("TGA","第三层的nickname数据:"+userInfoBean.getNickname());
Log.e("TGA","第三层的phone数据:"+userInfoBean.getPhone());

OVER

当使用Gson解析上述JSON数据时,首先需要定义一个Employee类来表示每个员工对象。然后可以使用Gson库将JSON数据解析为Employee对象的列表。以下是一个示例代码: ```java import com.google.gson.Gson; import java.util.List; public class MainActivity extends AppCompatActivity { private String json = "{\n" + " \"employees\": [\n" + " { \"firstName\":\"Bill\" , \"lastName\":\"Gates\" },\n" + " { \"firstName\":\"George\" , \"lastName\":\"Bush\" },\n" + " { \"firstName\":\"Thomas\" , \"lastName\":\"Carter\" }\n" + " ]\n" + "}"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 使用Gson解析JSON数据 Gson gson = new Gson(); EmployeeList employeeList = gson.fromJson(json, EmployeeList.class); // 遍历员工列表并输出 List<Employee> employees = employeeList.getEmployees(); for (Employee employee : employees) { Log.d("Employee", "First Name: " + employee.getFirstName() + ", Last Name: " + employee.getLastName()); } } } class Employee { private String firstName; private String lastName; // 必须提供默认构造函数 public Employee() { } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } } class EmployeeList { private List<Employee> employees; public List<Employee> getEmployees() { return employees; } } ``` 在上述代码,我们定义了一个Employee类表示每个员工对象,其包含了firstName和lastName字段。然后,我们定义了一个EmployeeList类表示员工列表,它包含了一个employees字段,用来存储多个员工对象。在MainActivity,我们使用Gson库的fromJson()方法将JSON数据解析为EmployeeList对象,并通过getEmployees()方法获取员工列表。最后,我们遍历员工列表并输出每个员工的姓名。 请注意,上述代码使用的是内部定义的JSON字符串,你可以将其替换为你实际获取到的JSON数据。同时,确保你在项目的build.gradle文件添加了Gson库的依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值