我有一个具有3个Timestamp字段的对象,我想使用Gson消耗json响应,但我有一个无效的时区指示符' &#39 ;. 所以这是出局:
*** Test Cases ***
TEST 1
[Teardown] log this teardown is unique to TEST 1
do some action
TEST 2
[Teardown] log this teardown is unique to TEST 2
do another action
这是我的Json回应:
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
}
这是我的Gson代码:
{
"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": [],
}
这是我的DateDeserializer
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.registerTypeAdapter(Timestamp.class, new DateDeserializer()).create();
invoiceModels = gson.fromJson(jsonObject.get("data").toString(), InvoiceModel[].class);
答案 0 :(得分:0)
您似乎在Z
中明确包含了时区模式片段"yyyy-MM-dd'T'HH:mm:ssZ'"
。从您的示例来看,您的数据不包含任何时区。
在格式化程序上设置时区并不意味着它将忽略已解析字符串的时区部分。相反,它将在时区数据不足时使用。
答案 1 :(得分:-1)
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.registerTypeAdapter(Timestamp.class, new DateDeserializer()). **.setDateFormat(DateFormatUtils.ISO_DATETIME_FORMAT.getPattern())**
create();
将起作用