6

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"

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.