一、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