我有一个JSON对象,其中包含以下值:
var json = [{
"Time": "2017-08-17 16:35:28.000",
"Value": "3.85"
}, {
"Time": "2017-08-17 17:36:28.000",
"Value": "3.85"
}, {
"Time": "2017-08-17 18:35:28.000",
"Value": "3.86"
}, {
"Time": "2017-08-17 19:35:28.000",
"Value": "3.86"
}, {
"Time": "2017-08-18 07:35:28.000",
"Value": "3.87"
}, {
"Time": "2017-08-18 18:35:28.000",
"Value": "3.86"
}];
时间是字符串格式,我想将其更改为日期时间。(我将它们存储在x和y中,我将其用于Highcharts) 我使用了以下功能:
json.forEach(function(element, index) {
element.x = new Date(element['Time']).getTime();
element.y = +element['Value'];
delete element['Time'];
delete element['Value'];
});
我收到错误:json.forEach不是函数
输出图片: 在Developer tools sources选项卡中: Image
在警报中: Alert
正如您所看到的,在开发人员工具中,它有引号,而在警报中它并没有。不知道问题是什么
PS:抱歉,我没有足够的声誉直接嵌入图片
答案 0 :(得分:0)
它是一个错字,从json声明中删除引号,你的解决方案将起作用
var json = '[{ "Time": "2017-08-17 16:35:28.000", "Value": "3.85"}, {"Time": "2017-08-17 17:36:28.000","Value": "3.85"}, {"Time": "2017-08-17 18:35:28.000","Value": "3.86"}, {"Time": "2017-08-17 19:35:28.000","Value": "3.86"}, {"Time": "2017-08-18 07:35:28.000","Value": "3.87"}, { "Time": "2017-08-18 18:35:28.000", "Value": "3.86"}]';
json = JSON.parse(json);
json.forEach(function(element, index) {
element.x = new Date(element['Time']).getTime();
element.y = +element['Value'];
delete element['Time'];
delete element['Value'];
});
console.log(json);