简体   繁体   English

通过将所有日期转换为ISOString格式将Javascript数组格式化为JSON

[英]Format Javascript Array into JSON by converting all dates into ISOString format

I have javascript array of objects with key and value in it. 我有对象的javascript数组,其中包含键和值。 I have so many dates in the array and I want to convert the entire array into proper JSON and in the process I also convert all dates inside array into ISOString format. 我在数组中有很多日期,我想将整个数组转换为正确的JSON,在此过程中,我还将数组中的所有日期转换为ISOString格式。 I can only use JQuery, UnderscoreJS or momentz libraries. 我只能使用JQuery,UnderscoreJS或momentz库。

Initial Format of my javascript array: 我的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"
}

Expected format: 预期格式:

{  
   "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"
}

This should do. 这应该做。 It uses regex to find the date values and JSON.stringify with a custom handler to put it all together. 它使用正则表达式查找日期值,并使用自定义处理程序将JSON.stringify组合在一起。 I would also like to point out that this will calculate the timezone as being whatever system timezone that this script is running on. 我还要指出的是,这会将时区计算为运行此脚本的任何系统时区。 As the output time will be in UTC you might want to make sure the timezone is correct beforehand. 由于输出时间将采用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; }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM