mikegreenberg
mikegreenberg

Reputation: 1421

Creating new JSON objects by accessing variable keys within another JSON object

I'm trying to take an existing JSON object and create smaller JSON chunks out of it specific for creating separate graphs. Here is my code which uses jQuery:

function updateStationGraphs (bsid) {
    $.getJSON("includes/system/ajaxDataInterface.php", {format:'flot',target:bsid}, function(data) {
        var graphOptions = {
            series: {
              lines: { show: true },
              points: { show: true }
            }
          };
        var handsetData = new Array(data[bsid].maxHandsets,data[bsid].avgHandsets);
        $.plot($('.graphHandsets'),handsetData,graphOptions);
    });

}

Clarification: handsetData is expecting something of the format:

  [ { label: "Foo", data: [ [10, 1], [17, -14], [30, 5] ] },
    { label: "Bar", data: [ [11, 13], [19, 11], [30, -7] ] } ]

My problem is when calling updateStationGraphs('A5A50000') for instance, it reports data[bsid] is undefined. I had a similar problem previously but my problem turned out to be that I was redeclaring my 'data' object again and was playing with the wrong object. This doesn't seem to be the case here and I can't figure out why it won't let me access data[bsid].

Here is what data is suppose to look like:

var data = {"A5A50000":{"time":{"label":"time","data":[[1244045863,"2009-06-03 16:17:43"],[1244045803,"2009-06-03 16:16:43"],[1244045743,"2009-06-03 16:15:43"],[1244045683,"2009-06-03 16:14:43"],[1244045623,"2009-06-03 16:13:43"],[1244045563,"2009-06-03 16:12:43"],[1244045503,"2009-06-03 16:11:43"],[1244045443,"2009-06-03 16:10:43"],[1244045383,"2009-06-03 16:09:43"],[1244045323,"2009-06-03 16:08:43"]]},"avgHandsets":{"label":"avgHandsets","data":[[1244045863,204.7143],[1244045803,205.9444],[1244045743,205.3333],[1244045683,205.3889],[1244045623,204.5882],[1244045563,204.8235],[1244045503,205],[1244045443,205.9412],[1244045383,205.6667],[1244045323,204.1176]]},"maxHandsets":{"label":"maxHandsets","data":[[1244045863,314],[1244045803,314],[1244045743,315],[1244045683,315],[1244045623,315],[1244045563,314],[1244045503,314],[1244045443,316],[1244045383,316],[1244045323,312]]}}};

Suggestions are greatly appreciated. Thank in advance.

Upvotes: 1

Views: 1640

Answers (2)

mikegreenberg
mikegreenberg

Reputation: 1421

In hindsight, after rest and a fresh start, I was able to realize I was accessing data[bsid] when bsid = 'a5a50000' instead of bsid = 'A5A50000'. ALWAYS CHECK YOUR CASE! sigh

Upvotes: 0

Jose Basilio
Jose Basilio

Reputation: 51488

I tested your code and every thing is fine. I get the flot graph generated correctly after the $.getJSON() call.

You need to make sure that the JSON data returned does contain the variable declaration and semicolon at the end as in your example.

In other words, it should be the JSON only.

{"A5A50000":{"time":{"label":"time","data":[[1244045863.........]}}}

Instead of:

var data = {"A5A50000":{"time":{"label":"time","data":[[1244045863,....

This may be the problem.

Upvotes: 2

Related Questions