나는 3 개 타임 스탬프 필드가 객체를하고 난 GSON를 사용하여 JSON 응답을 소비하고 싶지만 내가 잘못된 시간대 표시 '를'가지고있다.는 어떻게 GSON를 사용하여 타임 스탬프를 JSON 응답을 처리 할 수 ?? java.lang.IndexOutOfBoundsException
com.google.gson.JsonSyntaxException: 2017-11-30 15:19:37
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:84)
... 31 more Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator ' '
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)
... 32 more
Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator ' '
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)
... 32 more
을 그리고 이것은 내 JSON 응답입니다 : 은 그래서 이것은 밖으로 넣어
{
"status": "OK",
"data": {
"due_days": 10,
"attested_at": null,
"updated_at": "2017-12-01 11:59:56",
"automatic_collection": {
"amount": null,
"delay_days": null
},
"currency": "SEK",
"collector_paid_sum": 0,
"remaining_sum": 0
},
"id": "zeY37WJ",
"reminder_fee": 60.0,
"interest_rate": 8.5,
"state": "Unattested",
"due_date": "2017-12-11",
"automatic_reminders_settings": [],
"invoice_fee_vat": 0,
"invoice_date": "2017-12-01",
"reminder_count": 0,
"interest_fee": 0.0,
"invoice_no": 0,
"created_at": "2017-11-30 15:21:08",
"total_sum": 0.0,
"delivery_method": "Letter",
"remaining_sum": 0.0,
"flags": [],
}
}
여기가 내 GSON 코드 :
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.registerTypeAdapter(Timestamp.class, new DateDeserializer()).create();
invoiceModels = gson.fromJson(jsonObject.get("data").toString(), InvoiceModel[].class);
이 내 DateDeserializer입니다
public class DateDeserializer implements JsonDeserializer<Date> {
public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
String date = element.getAsString();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
return format.parse(date);
} catch (ParseException exp) {
System.out.println("Failed to parse Date:"+ exp);
return null;
}}}