我把这个字符串解析成一个 Javascript 对象。
{
"startTime": 233432420233,
"endTime": 233432431000,
"bufferingDelays": [
{
"time": 233432420233,
"delayLength": 100
},
{
"time": 233432420433,
"delayLength": 50
},
{
"time": 233432420833,
"delayLength": 75
}
]
}
这是执行解析然后转换为 JSON 字符串的 Javascript 代码:
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 );
JSON 转换产生的日期之一是:1977-05-25T18:20:20.233Z
. 我认为尾随的“Z”很糟糕。
现在在 Java 中,我尝试将其解析为 Java 对象,如下所示:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
Report report = gson.fromJson( jsonBuilder.toString(), Report.class );
但我得到了这个例外:
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"