creating a json object with variable fields
-
30-06-2021 - |
Frage
I am currently trying to use morris.js to create a graph. I am given one JSON object and need to make it look like another. Morris.js needs an array to look like this
var day_data = [
{"period": "2012-10-01", "licensed": 3407, "sorned": 660},
{"period": "2012-09-17", "licensed": 3171, "sorned": 660},
{"period": "2012-09-16", "licensed": 3171, "sorned": 676},
{"period": "2012-09-15", "licensed": 3201, "sorned": 656},
{"period": "2012-09-10", "licensed": 3215, "sorned": 622}
];
here is the link if the object appears confusing. It should be a shortened version of the first graph's data on their site http://oesmith.github.com/morris.js/ . Its pretty simple, period is the x coordinate, and licensed and sorned are y coordinates for their corresponding lines.
Here is the data I am given
var trendline = {"command":[
{"keyValuePairs":["2012-08-10 22:00:00|1884978","2012-08-10 21:00:00|3135378","2012-08-10 18:00:00|2541438","2012-08-09 20:00:00|647082","2012-08-10 19:00:00|3194772","2012-08-09 16:00:00|2782140","2012-08-10 20:00:00|3669924"],
"keyword":"Obama"},
{"keyValuePairs":["2012-08-10 22:00:00|1884978","2012-08-10 21:00:00|3135378","2012-08-10 18:00:00|2541438","2012-08-09 20:00:00|647082","2012-08-10 19:00:00|3194772","2012-08-09 16:00:00|2782140","2012-08-10 20:00:00|3669924"],
"keyword":"Romney"}]}
I need to extract all the data from keyValuePairs and associate it with its keyword so looks like the first array
{"period": "2012-10-01", "Obama": 1884978, "Romney": 1884978},
...
...
I know to get the data from the trendline JSON object but i don't know how to construct a new JSON object that the morris.js plugin could use. More specifically i don't know how to create a JSON object using variables.
trendline.dog = [cat];
Will create a json object with the field dog set to the value cat. However, i need to take each keyword(obama,romney...etc) and have a field associated with it and set a value to it. Since i don't know the number of fields or what their names are, I don't know how to create this object.
Assume I cannot change the way I am given data as I do not write that part. Also I could be given only two keywords (obama/romney) like in my example or i could be given any number of keywords for the y-values.
Sorry for the long length. Any help would be appreciated. Thanks for your time.
Lösung
Here is your Christmas present.
<script type="text/javascript">
var trendline =
{
"command": [
{
"keyValuePairs":
[
"2012-08-10 22:00:00|1884978",
"2012-08-10 21:00:00|3135378",
"2012-08-10 18:00:00|2541438",
"2012-08-09 20:00:00|647082",
"2012-08-10 19:00:00|3194772",
"2012-08-09 16:00:00|2782140",
"2012-08-10 20:00:00|3669924"
],
"keyword": "Obama"
},
{
"keyValuePairs":
[
"2012-08-10 22:00:00|1884978",
"2012-08-10 21:00:00|3135378",
"2012-08-10 18:00:00|2541438",
"2012-08-09 20:00:00|647082",
"2012-08-10 19:00:00|3194772",
"2012-08-09 16:00:00|2782140",
"2012-08-10 20:00:00|3669924"
],
"keyword": "Romney"
}]
}
var cmd = trendline.command,
day_data = [],
split,
date,
num,
obj;
//
for (var i = 0; i < cmd.length; i++) {
//
if (i == 1) { break; };
//
for (var ii = 0; ii < cmd[i].keyValuePairs.length; ii++) {
//debugger
split = cmd[i].keyValuePairs[ii].split('|');
date = split[0].substring(0, split[0].indexOf(' '));
num = split[1];
obj = {};
obj['period'] = date;
//Can 1
obj[cmd[i].keyword] = num;
//
split = cmd[i + 1].keyValuePairs[ii].split('|');
num = split[1];
//Can 2
obj[cmd[i + 1].keyword] = num;
//
day_data.push(obj);
};
};