Reputation: 3912
I take this String and parse it to a Javascript object.
{
"startTime": 233432420233,
"endTime": 233432431000,
"bufferingDelays": [
{
"time": 233432420233,
"delayLength": 100
},
{
"time": 233432420433,
"delayLength": 50
},
{
"time": 233432420833,
"delayLength": 75
}
]
}
Here is the Javascript code doing the parsing followed by the conversion to a JSON string:
var reportObject = jQuery.parseJSON(reportJSONString);
reportObject.startTime = new Date(reportObject.startTime);
reportObject.endTime = new Date(reportObject.endTime);
for (var i = 0; i < reportObject.bufferingDelays.length; i++)
{
var delay = reportObject.bufferingDelays[i];
delay.time = new Date( delay.time );
reportObject.bufferingDelays[i] = delay;
}
var reportObjectFinalString = JSON.stringify( reportObject );
One of the dates produced by the JSON conversion is this: 1977-05-25T18:20:20.233Z
. I think the trailing 'Z' is bad.
Now in Java I attempt to parse it into a Java Object like so:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
Report report = gson.fromJson( jsonBuilder.toString(), Report.class );
But I get this exception:
SEVERE: Servlet.service() for servlet [ReportServlet] in context with path [/Report] threw exception [com.google.gson.JsonSyntaxException: 1977-05-25T18:20:20.233Z] with root cause
java.text.ParseException: Unparseable date: "1977-05-25T18:20:20.233Z"
Upvotes: 6
Views: 8260
Reputation: 279970
You need to quote the Z
too
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
The SimpleDateFormat
(used in the GsonBuilder
) takes the unquoted Z
to mean a time zone which your date string doesn't have.
Upvotes: 10