如果以集合的方式就复制全部数据进行解析,如果是列表方式,就直接复制其中一个元素去解析 如果是集合就是UserinfoBean userInfo这个形式,如果数据是列表,就是List list这个形式
data以集合方式 data{id:“1”,uid:“5”} 元素嵌套列表 data{list:[coin:“1”,level:“2”],“tips”: “”} data以数组方式 data[{id:“1”,image:“25”},{}] 元素嵌套列表 data[{id:“1”,userinfo:{id:"152,name:“652”}} , {} , {}]
1.如下 2.通用的解析做法就是直接复制数组里面的一个元素进行解析就可以了,中括号都可以不用,下面括起来的可以不用解析的,因为服务器已经进行解析了的,就直接解析[]里面的一个元素就可以了
data[{id:“1”,userinfo:{id:"152,name:“652”}} , {} , {}]
3.从服务器返回的数据大概有几种类型 第一种就是数组里面有好多个元素,元素里面又有列表 data[{id:“1”,userinfo:{id:"152,name:“652”}} , {} , {}]
package com.dimi.app.data;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.List;
public class Dynamic implements Serializable {
@SerializedName("id")
private int id;
@SerializedName("uid")
private int uid;
@SerializedName("title")
private String title;
@SerializedName("addtime")
private int addTime;
@SerializedName("video_thumb")
private String videoThumb;
@SerializedName("href")
private String href;
@SerializedName("city")
private String city;
@SerializedName("voice")
private String voice;
@SerializedName("length")
private int length;
@SerializedName("likes")
private int likes;
@SerializedName("comments")
private int comments;
@SerializedName("type")
private int type;
@SerializedName("isattent")
private int isAttent;
@SerializedName("userinfo")
private UserinfoBean userInfo;
@SerializedName("pics")
private List<String> pics;
@SerializedName("thumbs")
private List<String> thumbs;
public int getId() {
return id;
}
public int getUid() {
return uid;
}
public String getTitle() {
return title;
}
public int getAddTime() {
return addTime;
}
public String getVideoThumb() {
return videoThumb;
}
public String getHref() {
return href;
}
public String getCity() {
return city;
}
public String getVoice() {
return voice;
}
public int getLength() {
return length;
}
public int getLikes() {
return likes;
}
public int getComments() {
return comments;
}
public int getType() {
return type;
}
public boolean isAttent() {
return isAttent == 1;
}
public UserinfoBean getUserInfo() {
return userInfo;
}
public List<String> getPics() {
return pics;
}
public List<String> getThumbs() {
return thumbs;
}
public static class UserinfoBean implements Serializable {
@SerializedName("id")
private int id;
@SerializedName("user_nickname")
private String userNickname;
@SerializedName("avatar")
private String avatar;
@SerializedName("avatar_thumb")
private String avatarThumb;
@SerializedName("sex")
private int sex;
@SerializedName("birthday")
private int birthday;
@SerializedName("isauth")
private int isAuth;
public int getId() {
return id;
}
public String getUserNickname() {
return userNickname;
}
public String getAvatar() {
return avatar;
}
public String getAvatarThumb() {
return avatarThumb;
}
public int getSex() {
return sex;
}
public int getBirthday() {
return birthday;
}
public int getIsAuth() {
return isAuth;
}
}
}
data{list:[coin:“1”,level:“2”],“tips”: “”}
还有一种是如下,一个集合里面有两个元素,其中一个元素是列表,直接复制data里面的全部元素去解析就可以了 data{list:[coin:“1”,level:“2”],“tips”: “”}
解析出的数据如下
package com.dimi.app.data;
import com.google.gson.annotations.SerializedName;
import java.util.List;
import androidx.annotation.NonNull;
public class MoviePrice{
@SerializedName("tips")
private String tips;
@SerializedName("list")
private List<Fee> list;
public String getTips() {
return tips;
}
public List<Fee> getList() {
return list;
}
public static class Fee {
@SerializedName("coin")
private int coin;
@SerializedName("level")
private int level;
@SerializedName("canselect")
private int canSelect;
public int getCoin() {
return coin;
}
public int getLevel() {
return level;
}
public Boolean getCanSelect() {
return canSelect==1;
}
@NonNull
@Override
public String toString() {
return String.valueOf(coin);
}
}
}
data{id:“1”,uid:“5”}
有一些直接就是data{id:“1”,uid:“5”},一个集合没有嵌套,就直接把里面的数据拿去解析就可以了,解析出的数据如下
package com.dimi.app.data;
import com.google.gson.annotations.SerializedName;
public class BuyPhoto{
@SerializedName("id")
private int id;
@SerializedName("uid")
private int uId;
@SerializedName("thumb")
private String thumb;
@SerializedName("thumb_s")
private String thumbs;
@SerializedName("views")
private int views;
@SerializedName("isprivate")
private int isPrivate;
@SerializedName("coin")
private int coin;
public int getId() {
return id;
}
public int getuId() {
return uId;
}
public String getThumb() {
return thumb;
}
public String getThumbs() {
return thumbs;
}
public int getViews() {
return views;
}
public int getIsPrivate() {
return isPrivate;
}
public int getCoin() {
return coin;
}
}
data[{id:“1”,image:“25”},{}]
还有一种是data[{id:“1”,image:“25”},{}],数组里面就是以集合为单位的元素,直接拿里面一个元素去解析就可以了 解析结果如下
package com.dimi.app.data;
import com.google.gson.annotations.SerializedName;
import androidx.annotation.NonNull;
public class HomeBanner{
@SerializedName("image")
private String image;
@SerializedName("url")
private String url;
public String getImage() {
return image;
}
public String getUrl() {
return url;
}
public HomeBanner setImage(String image) {
this.image = image;
return this;
}
public HomeBanner setUrl(String url) {
this.url = url;
return this;
}
@NonNull
@Override
public String toString() {
return "HomeBanner{" +
"image='" + image + '\'' +
", url='" + url + '\'' +
'}';
}
}
|