我有javascript对象的数组,其中包含键和值。我在数组中有很多日期,我想将整个数组转换为正确的JSON,在此过程中我还将数组中的所有日期转换为ISOString格式。我只能使用JQuery,UnderscoreJS或momentz库。
我的javascript数组的初始格式:
{
"primaryPerformerId":"122418",
"primaryGroupingId":"63913",
"primaryCategoryId":"1",
"name":"Test Concert Event",
"venueId":"82",
"placeConfigs":[
{
"placeConfigId":"1232392"
}
],
"defaultLocale":"en_US",
"metas":[
{
"templateId":"201",
"name":"Test Concert Event",
"locale":"en_US"
}
],
"unknownEventDateIndicator":"false",
"unknownEventTimeIndicator":"false",
"eventStartTime":"05/18/2016 08:04 PM",
"trueOnSaleDate":"05/18/2016 08:04 PM",
"firstPresaleDate":null,
"status":"active",
"dynamicAttributes":[
],
"lastChanceDate":"05/18/2016 08:04 PM",
"onSaleDate":"05/15/2016 08:04 PM",
"confirmDate":"05/16/2016 08:04 PM",
"earliestPossibleInhandDate":"05/16/2016 08:04 PM",
"latestPossibleInhandDate":"05/18/2016 08:04 PM"
}
预期格式:
{
"primaryPerformerId":"122418",
"primaryGroupingId":"63913",
"primaryCategoryId":"1",
"name":"Test Concert Event",
"venueId":"82",
"placeConfigs":[
{
"placeConfigId":"1232392"
}
],
"defaultLocale":"en_US",
"metas":[
{
"templateId":"201",
"name":"Test Concert Event",
"locale":"en_US"
}
],
"unknownEventDateIndicator":"false",
"unknownEventTimeIndicator":"false",
"eventStartTime":"2016-05-18T20:04:00.000Z",
"trueOnSaleDate":"2016-05-17T20:03:00.000Z",
"firstPresaleDate":null,
"status":"active",
"dynamicAttributes":[
],
"lastChanceDate":"2016-05-18T20:04:00.000Z",
"onSaleDate":"2016-05-12T23:38:18.775Z",
"confirmDate":"2016-05-11T23:38:18.775Z",
"earliestPossibleInhandDate":"2016-05-10T20:04:00.000Z",
"latestPossibleInhandDate":"2016-05-11T20:04:00.000Z"
}
答案 0 :(得分:1)
这应该做。它使用正则表达式来查找日期值,并使用自定义处理程序将JSON.stringify
放在一起。我还想指出,这会将时区计算为运行此脚本的任何系统时区。由于输出时间为UTC,您可能需要事先确保时区正确。
var o = {
"primaryPerformerId":"122418",
"primaryGroupingId":"63913",
"primaryCategoryId":"1",
"name":"Test Concert Event",
"venueId":"82",
"placeConfigs":[
{
"placeConfigId":"1232392"
}
],
"defaultLocale":"en_US",
"metas":[
{
"templateId":"201",
"name":"Test Concert Event",
"locale":"en_US"
}
],
"unknownEventDateIndicator":"false",
"unknownEventTimeIndicator":"false",
"eventStartTime":"05/18/2016 08:04 PM",
"trueOnSaleDate":"05/18/2016 08:04 PM",
"firstPresaleDate":null,
"status":"active",
"dynamicAttributes":[
],
"lastChanceDate":"05/18/2016 08:04 PM",
"onSaleDate":"05/15/2016 08:04 PM",
"confirmDate":"05/16/2016 08:04 PM",
"earliestPossibleInhandDate":"05/16/2016 08:04 PM",
"latestPossibleInhandDate":"05/18/2016 20:04"
};
document.body.innerText = JSON.stringify(o, function(key, value) {
var res;
if(res = /^\s*([0-9]{1,2})\s*\/\s*([0-9]{1,2})\s*\/\s*([0-9]{1,4})\s+([0-9]{1,2})\s*\:\s*([0-9]{1,2})(?:\s*(AM|PM))?\s*$/i.exec(value)) {
value = (o.defaultLocale === 'en_US' ?
new Date(res[3], res[1]-1, res[2], res[6] ? (res[6].toUpperCase() === 'PM' ? 12 : 0) + (res[4] === '12' ? 0 : parseInt(res[4])) : res[4], res[5]) :
new Date(res[3], res[2]-1, res[1], res[6] ? (res[6].toUpperCase() === 'PM' ? 12 : 0) + (res[4] === '12' ? 0 : parseInt(res[4])) : res[4], res[5])
).toISOString();
}
return value;
});