简体   繁体   中英

How should I define my model class for an arrangement with Gson?

I have the following 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"
    }
}

Tell them to receive and work with JSON GSON me to believe an object, the probleam is that defined the object follows

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;
}

}

How should I define my model to get the "happy_hours" data?

StoreModel类将包含happy_hours的对象

Create a HappyHours class with appropriate attributes and add an attribute happyHours to your StoreModel :

@SerializedName("happy_hours")
private HappyHours happyHours;

For Date objects try "Date start;" and "Date end;" If it doesn't work, you have to write an adapter :

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())
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM