2014-01-24 46 views
0

我有以下JSON:我應該如何定義我的模型類與Gson的安排?

{ 
    _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我努力相信的對象時,probleam是,定義的對象如下

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

可以使消氣setter方法「happy_hour」以類似的方式爲你做其他的鍵。 –

回答

1

StoreModel類將包含happy_hours的對象

+0

你會如何輸入屬性「開始」和「結束」? – cheloncio

1

創建具有相應屬性的HappyHours類,並添加屬性happyHours您StoreModel:

@SerializedName("happy_hours") 
private HappyHours happyHours; 

爲Date對象嘗試 「日期開始;」和「日期結束」; 如果它不工作,你必須寫一個適配器:

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

} 
+0

你會如何輸入屬性「開始」和「結束」? – cheloncio

+0

我編輯了我的答案。 – Thierry

相關問題