简体   繁体   English

用可变字段创建一个json对象

[英]creating a json object with variable fields

I am currently trying to use morris.js to create a graph. 我目前正在尝试使用morris.js创建图形。 I am given one JSON object and need to make it look like another. 我得到了一个JSON对象,需要使其看起来像另一个对象。 Morris.js needs an array to look like this Morris.js需要一个数组看起来像这样

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/ . 它应该是其站点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. 它非常简单,句点是x坐标,许可的和残缺的是它们相应行的y坐标。

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 我需要从keyValuePairs中提取所有数据,并将其与其关键字关联,以便看起来像第一个数组

{"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. 我知道要从趋势线JSON对象获取数据,但是我不知道如何构造morris.js插件可以使用的新JSON对象。 More specifically i don't know how to create a JSON object using variables. 更具体地说,我不知道如何使用变量创建JSON对象。

trendline.dog = [cat];

Will create a json object with the field dog set to the value cat. 将创建一个json对象,并将字段dog设置为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. 但是,我需要接受每个关键字(obama,romney等),并与之关联的字段并为其设置一个值。 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. 也可以像我的示例那样仅给我两个关键字(奥巴马/罗姆尼),也可以给我任意数量的y值关键字。

Sorry for the long length. 很抱歉,长度太长。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks for your time. 谢谢你的时间。

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);
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM