I've passed a Java HashMap as JSON to the client side. The resulting JSON is as follows:
我已经将Java HashMap作为JSON传递给客户端。得到的JSON如下:
{
"2013-02-27T07:25:35.000+0000": 40,
"2013-03-01T07:25:35.000+0000": 33,
"2013-02-26T07:25:35.000+0000": 25,
"2013-02-23T07:25:35.000+0000": 54,
"2013-03-03T10:12:59.000+0000": 26,
"2013-03-02T07:12:59.000+0000": 25
}
But for plotting onto Flot charts I need the input in the following format:
但是为了绘制Flot图表,我需要以下格式的输入:
[
[1328983200000, 40],
[1328983200000, 33],
[1328983200000, 25],
[1328983200000, 54],
[1328983200000, 26],
[1328983200000, 25]
];
where the first value is Unix Timestamp X 1000
第一个值是Unix时间戳X 1000
(The time series support in Flot is based on Javascript timestamps, i.e. everywhere a time value is expected or handed over, a Javascript timestamp number is used. This is a number, not a Date object. A Javascript timestamp is the number of milliseconds since January 1, 1970 00:00:00 UTC. This is almost the same as Unix timestamps, except it's in milliseconds, so remember to multiply by 1000!)
Flot中的时间序列支持是基于Javascript时间戳的,即在期望或提交时间值的任何地方,都使用Javascript时间戳号。这是一个数字,而不是一个日期对象。Javascript时间戳是自1970年1月1日00:00 UTC开始的毫秒数。这几乎和Unix时间戳一样,只不过它是以毫秒为单位的,所以记住要乘以1000!)
How can I convert it? Can anyone kindly guide. :(
怎么转换呢?谁能请指导。:(
2 个解决方案
#1
2
var list = {
"2013-02-27T07:25:35.000+0000": 40,
"2013-03-01T07:25:35.000+0000": 33,
"2013-02-26T07:25:35.000+0000": 25,
"2013-02-23T07:25:35.000+0000": 54,
"2013-03-03T10:12:59.000+0000": 26,
"2013-03-02T07:12:59.000+0000": 25
}, arr = [];
for (var key in list) {
arr.push([+new Date(key)*100, list[key]]); //One simple line of code
} //Keep trying!
console.log(arr);
http://jsfiddle.net/DerekL/F4tst/
http://jsfiddle.net/DerekL/F4tst/
#2
2
What you could do is iterate through the object, create a Date object out of the date you have, use the getTime() method to get the timestamp in milliseconds, create a new array, then push it onto your main array. This may not be the best solution, but it will work.
您可以做的是遍历对象,在已有的日期之外创建一个日期对象,使用getTime()方法以毫秒为单位获取时间戳,创建一个新数组,然后将其推到主数组中。这可能不是最好的解决方案,但它会起作用。
Assuming your JSON object is named obj;
假设您的JSON对象名为obj;
var mainArray = [];
for (var x in obj) {
var tmpDate = (new Date(x)).getTime();
var smallArray = [tmpDate, obj[x]];
mainArray.push(smallArray);
}
#1
2
var list = {
"2013-02-27T07:25:35.000+0000": 40,
"2013-03-01T07:25:35.000+0000": 33,
"2013-02-26T07:25:35.000+0000": 25,
"2013-02-23T07:25:35.000+0000": 54,
"2013-03-03T10:12:59.000+0000": 26,
"2013-03-02T07:12:59.000+0000": 25
}, arr = [];
for (var key in list) {
arr.push([+new Date(key)*100, list[key]]); //One simple line of code
} //Keep trying!
console.log(arr);
http://jsfiddle.net/DerekL/F4tst/
http://jsfiddle.net/DerekL/F4tst/
#2
2
What you could do is iterate through the object, create a Date object out of the date you have, use the getTime() method to get the timestamp in milliseconds, create a new array, then push it onto your main array. This may not be the best solution, but it will work.
您可以做的是遍历对象,在已有的日期之外创建一个日期对象,使用getTime()方法以毫秒为单位获取时间戳,创建一个新数组,然后将其推到主数组中。这可能不是最好的解决方案,但它会起作用。
Assuming your JSON object is named obj;
假设您的JSON对象名为obj;
var mainArray = [];
for (var x in obj) {
var tmpDate = (new Date(x)).getTime();
var smallArray = [tmpDate, obj[x]];
mainArray.push(smallArray);
}