我有以下JSON:
{
_id: "5252fdf424f1e7fbf7000004",
address: "Calle 1000",
city: "Concepción",
created_at: "2013-10-07T18:31:19.375Z",
description: "",
name: "Joctos",
phone: "94967994",
updated_at: "2013-12-09T13:03:07.328Z",
happy_hour: {
active: false,
type: 1,
all_day: false,
start: "2013-12-17T03:30:00.000Z",
end: "2013-12-17T05:00:00.000Z"
}
}
告诉他们接收和使用JSON GSON我相信一个对象,问题是定义对象如下
public class StoreModel {
@SerializedName("_id")
private String _id;
@SerializedName("address")
private String address;
@SerializedName("city")
private String city;
@SerializedName("created_at")
private String created_at;
@SerializedName("description")
private String description;
@SerializedName("name")
private String name;
@SerializedName("phone")
private String phone;
@SerializedName("updated_at")
private String updated_at;
public String get_id() {
return this._id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getCreated_at() {
return this.created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUpdated_at() {
return this.updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
如何定义模型以获取“happy_hours”数据?
答案 0 :(得分:1)
StoreModel类将包含happy_hours的对象
答案 1 :(得分:1)
使用适当的属性创建HappyHours类,并将属性happyHours添加到StoreModel:
@SerializedName("happy_hours")
private HappyHours happyHours;
对于Date对象,请尝试“Date start;”和“日期结束;” 如果它不起作用,你必须编写一个适配器:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateGsonDeserializer());
gsonBuilder.create();
public class DateGsonDeserializer implements JsonDeserializer<Date> {
@Override
public Date deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
// just write the right formatter from SimpleDateFormat
return formatToDate(jsonElement.getAsString())
}
}