I am using Gson to parse value from response. My response looks like (In postman):
{
"payLoad": {
"totalItems": 1,
"totalPages": 1,
"items": [
{
"id": "f13e9f94fbb144d2956326f6c50f4d29",
"rechargeStatus": "PENDING",
"dateOfRequest": "2017-05-30T11:29:33",
"amount": 1,
"category": "Electricity",
"operator": "Electricity Limited",
"whereToRecharge": "212323232"
}
]
},
"success": true,
"timestamp": "2017-05-30T13:08:18"
}
When I log my response in AS
, I have:
Fetch Recharge Payload : {totalItems=1.0, totalPages=1.0, items=
[{uuid=f12e9f94fbb144d2956326f6c50f4d29, rechargeStatus=PENDING,
dateOfRequest=2017-05-30T11:29:33, amount=1.0, category=Electricity,
operator=Electricity Limited, whereToRecharge=212323232}]}
I have already added GsonBuilder
for date format as follows (as sugessted here):
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
Parsing data looks like:
Map<String, Object> map = (Map<String, Object>) myResponse.payLoad;
List<Recharge> rechargeTable = stringToArray(String.valueOf(map.get("items")), Recharge[].class);
...
public static <T> List<T> stringToArray(String s, Class<T[]> clazz) {
T[] arr = new Gson().fromJson(s, clazz);
return Arrays.asList(arr);
}
Error I get is:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException:
Unterminated object at line 1 column 94 path $[0].dateOfRequest
Main question is to parse date. Do I need to add some more configuration?
Extra Questions: - Why my integers are converted to double? - Why double quotes are removed from the String values?