Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Parse, JSON Stringify & GSON Parse of Date Fails

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"
like image 335
Guido Anselmi Avatar asked Sep 27 '13 01:09

Guido Anselmi


People also ask

Can I JSON Stringify a string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

What is JSON parse and Stringify?

parse() is used for parsing data that was received as JSON; it deserializes a JSON string into a JavaScript object. JSON. stringify() on the other hand is used to create a JSON string out of an object or array; it serializes a JavaScript object into a JSON string.

What does JSON Stringify () do?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

What is JSON parse JSON Stringify obj ))?

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.


1 Answers

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.

like image 94
Sotirios Delimanolis Avatar answered Sep 19 '22 00:09

Sotirios Delimanolis