我有一个从服务器收到的数据。
{
"_id": "5a78263eab294307bebc2e6b",
"userID": "PvIMsjink1UcrwXZ4gb899W5Kxo2",
"name": "battery",
"price": 22,
"description": "t",
"__v": 0,
"boardVisibility": "MainBoard",
"date": "2018-02-05T09:39:10.597Z",
"picture": {
"url": "https:\/\/firebasestorage.googleapis.com\/v0\/b\/bitbuy-bitbuy.appspot.com\/o\/user%2FPvIMsjink1UcrwXZ4gb899W5Kxo2%2FDepositphotos_59984669_s-2015.jpg?alt=media&token=a562fd6d-98ff-4fb8-ad93-c251a5016b0d",
"name": "Depositphotos_32026397_m-2015.jpg"
}
}
这是Model类:
@SerializedName("_id")
private String _id;
@SerializedName("userID")
private String userID;
@SerializedName("name")
private String name;
@SerializedName("price")
private double price;
@SerializedName("description")
private String description;
@SerializedName("date")
private String date;
@SerializedName("picture")
private JSONObject picture;
以下是我将数据设置为ArrayList
的方式:
userItemsArr = Gson().fromJson<List<UserItem>>(gsonObj.getJSONArray("docs").toString(), turnsType) as ArrayList<UserItem>
问题是响应中的picture
值包含另一个JSONObject,因此在我的模型类中,我得到一个空的JSONObject。
答案 0 :(得分:1)
"picture": {
"url": "https:\/\/firebasestorage.googleapis.com\/v0\/b\/bitbuy-bitbuy.appspot.com\/o\/user%2FPvIMsjink1UcrwXZ4gb899W5Kxo2%2FDepositphotos_59984669_s-2015.jpg?alt=media&token=a562fd6d-98ff-4fb8-ad93-c251a5016b0d",
"name": "Depositphotos_32026397_m-2015.jpg"
}
您应纠正 MODEL
类。
@SerializedName("picture")
@Expose
private Picture picture;
// Set-Get Method
public Picture getPicture() {
return picture;
}
public void setPicture(Picture picture) {
this.picture = picture;
}
和
public class Picture {
@SerializedName("url")
@Expose
private String url;
@SerializedName("name")
@Expose
private String name;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
您可以使用jsonschema2pojo
。