Converting a String into a Javascript object can be done using the following code:
{
"startTime": 233432420233,
"endTime": 233432431000,
"bufferingDelays": [
{
"time": 233432420233,
"delayLength": 100
},
{
"time": 233432420433,
"delayLength": 50
},
{
"time": 233432420833,
"delayLength": 75
}
]
}
The parsing and conversion process is shown in the Javascript code snippet below:
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 );
If you encounter an issue with dates having trailing 'Z' when converting to JSON, consider revising the format.
In Java, the parsing of the JSON data into a Java Object can be achieved using the Gson library as demonstrated below:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
Report report = gson.fromJson( jsonBuilder.toString(), Report.class );
However, ensure that the date format matches for successful conversion without exceptions.