I've passed a Java HashMap as JSON to the client side. The resulting JSON is as follows:
{
"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:
[
[1328983200000, 40],
[1328983200000, 33],
[1328983200000, 25],
[1328983200000, 54],
[1328983200000, 26],
[1328983200000, 25]
];
where the first value is Unix Timestamp X 1000
(The time series support in Flot is based on Javascript timestamps, ie 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!)
How can I convert it? Can anyone kindly guide. :(
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.
Assuming your JSON object is named obj;
var mainArray = [];
for (var x in obj) {
var tmpDate = (new Date(x)).getTime();
var smallArray = [tmpDate, obj[x]];
mainArray.push(smallArray);
}
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.