Reputation: 51
I am having trouble in serializing a json that has a date.
I have looked over a few other questions and they don't seem to help.
This is an sample of the code
String jsonString = { Date=2016-12-09T11:58:31 }
Gson myGson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
MyClass myClass = myGson.fromJson(jsonString, MyClass.class);
I get the following stack trace
Process: com.apacheapps.prang, PID: 22543
com.google.gson.JsonSyntaxException: 2016-12-09T11
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.text.ParseException: Unparseable date: "2016-12-09T11" (at offset 13)
at java.text.DateFormat.parse(DateFormat.java:579)
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
The java.text error is Unparseable date - "2016-12-09T11" (at offset 13) which makes me think that the date is being cut off at the colon between HH:mm.
I have done the exact same method on a number of times in different activities using the same class and it works fine. This is completely baffling me.
I would appreciate any help.
Thanks
Edit
As pointed out by ΦXocę and Monish, by json uses equals rather than key-val notation.
This is causing the issue.
For brevity I didn't initially explain the complete process. This is how it looks.
The response from the api looks like this
{"Status":true,"RecordsAffected":1,"Message":null,"OperationId":null,"Result":{"ID":2177,"Date":"2016-12-09T12:33:45"},"ErrorCode":0,"ExceptionMessage":null,"ExceptionStackTrace":null,"ExceptionInnerMessage":null,"ExceptionInnerStackTrace":null}
This is serialized into a JsonWrapper class
public class JsonWrapper{
public Object Result;
boolean Status;
public String getDisplayName() {
return Result.toString();
}
}
getDisplayName returns the below
{ID=2177.0, Date=2016-12-09T12:33:45}
I then go on to serialize this into the necessary class.
This normally serialises to the standard key-val notation.
Upvotes: 0
Views: 1516
Reputation: 1488
It is working perfectly fine. Your JSON is not valid.
Just change this line
String jsonString = { Date=2016-12-09T11:58:31 }
to
String jsonString = "{\"Date\":\"2016-12-09T11:58:31\"}";
String jsonString = "{\"Date\":\"2016-12-09T11:58:31\"}";
Gson myGson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
MyClass myClass = myGson.fromJson(jsonString, MyClass.class);
Log.d(getClass().getSimpleName(),"GSON deserialized date : " + myClass.getDate().toString());
Model class :
public class MyClass {
@SerializedName("Date")
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
Output : GSON deserialized date : Fri Dec 09 11:58:31 GMT+05:30 2016
Upvotes: 1