質問

I'm dealing with a fairly complex JSON array, included below. I'm trying to create an array of the Country objects with their included visits. Heres a sample of the array:

{
   "data":[
      {
         "period":"Month",
         "start_date":"2012-06",
         "end_date":"2012-07",
         "attributes":{

         },
         "measures":{
            "Visits":1000000
         },
         "SubRows":[
            {
               "Unknown":{
                  "measures":{
                     "Visits":1000
                  },
                  "SubRows":null
               },
               "**":{
                  "measures":{
                     "Visits":1000
                  },
                  "SubRows":null
               },
               "Afghanistan":{
                  "measures":{
                     "Visits":1000
                  },
                  "SubRows":null
               },
               "Aland Islands":{
                  "measures":{
                     "Visits":1000
                  },
                  "SubRows":null
               },
               "Albania":{
                  "measures":{
                     "Visits":100
                  },
                  "SubRows":null
               },
            }
         ]
      }
   ]
}

I'm using data[0].SubRows[0] to get down to an array of the country objects, but now I'm stumped how to go one below as each sub object is named differently? My intended output is for the Google Visualisation API, as follows:

var data = google.visualization.arrayToDataTable([
  ['Country', '% Visits'],
    ['United Kingdom', 1000],
    ['United States', 1000],
    ['France', 1000],
    ['Australia', 1000],
]);
役に立ちましたか?

解決

Glibnes is getting there with the for in suggestion, sticking to your variable names (BTW: in JS, these are objects, not arrays... well, arrays are objects, but I'm not going to be that pedantic).

var measures = {};
for (var cName in data[0].SubRows[0])//your country objects
{//it's always a good idea to check if a property is set directly to the object
 //if not, inheritance chain properties will show up here, too
    if (data[0].SubRows[0].hasOwnProperty(cName))
    {
        //assuming this object will Always be set, if not: check first
        measures[cName] = data[0].SubRows[0][cName].measures.Visits;
    }
}

measures will now be a one level object, where the countries are the keys and the values are the Visits. Feel free to add aditional checks, or use the values directly to build content or... whatever

What you really need, if I understand correctly though, is an array of arrays:

var result = [['Country', '% Visits']];
for (var cName in data[0].SubRows[0])//your country objects
{
    if (data[0].SubRows[0].hasOwnProperty(cName))
    {
        result.push([cName,data[0].SubRows[0][cName].measures.Visits]);
    }
}

result will now be an array, consisting of arrays, each with 2 values. result[x][0] -> country and result[x][1] -> value of measures.Visits:

result[1][0] === 'United Kingdom' && result[1][1] == 1000

他のヒント

Use For... In Statement:

var Country = JSONObject['data']['SubRows'];
var Data = {};
for(name in Country)
{
    Data[name] = Country[name]['measures']['Visits'];
}

It should be easy to match this to your needs.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top