在 JSON 数组中搜索时间并将其转换为 'epochtime'

Search for time in a JSON array and convert it to 'epochtime'

如何在如下所示的 JSON 数组中找到 "time" 的值:

{
   "data": [{
               "temperature": "20.0",
               "time": "2019-04-23 12:45:00+02:00"
           }]
}

并将此值转换为如下所示的纪元时间:

{
    "data": [{
                "temperature": "20.0",
                "timestamp": 1556016300000
            }]
}

使用Date.parse

  • 解析对象然后获取时间

  • 然后使用Date.parse()

  • 解析日期
  • 再次将对象字符串化

a = JSON.parse('{"data": [{"temperature": "20.0","time": "2019-04-23 12:45:00+02:00"}]}')
console.log(a)
a.data[0].time = Date.parse(a.data[0].time);
JSON.stringify(a);
console.log(a)