JSON - 访问JSON数组并分配变量

时间:2014-12-05 06:52:45

标签: javascript jquery arrays json getjson

我在名为census.json的文件中有以下JSON:

{
    "areas": [
        "UnitedStates", [{
        "STATEORREGION": "United States",
            "1910POPULATION": 92228531,
            "1920POPULATION": 106021568,
            "1930POPULATION": 123202660,
            "1940POPULATION": 132165129,
            "1950POPULATION": 151325798,
            "1960POPULATION": 179323175,
            "1970POPULATION": 203211926,
            "1980POPULATION": 226545805,
            "1990POPULATION": 248709873,
            "2000POPULATION": 281421906,
            "2010POPULATION": 308745538,
            "1910CHANGE": 21,
            "1920CHANGE": 15,
            "1930CHANGE": 16.2,
            "1940CHANGE": 7.3,
            "1950CHANGE": 14.5,
            "1960CHANGE": 18.5,
            "1970CHANGE": 13.3,
            "1980CHANGE": 11.5,
            "1990CHANGE": 9.8,
            "2000CHANGE": 13.2,
            "2010CHANGE": 9.7
    }],
        "Alabama", [{
        "STATEORREGION": "Alabama",
            "1910POPULATION": 2138093,
            "1920POPULATION": 2348174,
            "1930POPULATION": 2646248,
            "1940POPULATION": 2832961,
            "1950POPULATION": 3061743,
            "1960POPULATION": 3266740,
            "1970POPULATION": 3444165,
            "1980POPULATION": 3893888,
            "1990POPULATION": 4040587,
            "2000POPULATION": 4447100,
            "2010POPULATION": 4779736,
            "1910CHANGE": 16.9,
            "1920CHANGE": 9.8,
            "1930CHANGE": 12.7,
            "1940CHANGE": 7.1,
            "1950CHANGE": 8.1,
            "1960CHANGE": 6.7,
            "1970CHANGE": 5.4,
            "1980CHANGE": 13.1,
            "1990CHANGE": 3.8,
            "2000CHANGE": 10.1,
            "2010CHANGE": 7.5
    }],
        "Alaska", [{
        "STATEORREGION": "Alaska",
            "1910POPULATION": 64356,
            "1920POPULATION": 55036,
            "1930POPULATION": 59278,
            "1940POPULATION": 72524,
            "1950POPULATION": 128643,
            "1960POPULATION": 226167,
            "1970POPULATION": 300382,
            "1980POPULATION": 401851,
            "1990POPULATION": 550043,
            "2000POPULATION": 626932,
            "2010POPULATION": 710231,
            "1910CHANGE": 1.2,
            "1920CHANGE": -14.5,
            "1930CHANGE": 7.7,
            "1940CHANGE": 22.3,
            "1950CHANGE": 77.4,
            "1960CHANGE": 75.8,
            "1970CHANGE": 32.8,
            "1980CHANGE": 33.8,
            "1990CHANGE": 36.9,
            "2000CHANGE": 14,
            "2010CHANGE": 13.3
    }], ]
}

我试图使用以下内容访问存储在数组中的不同值:

$.getJSON("../data/census.json", function (json) {
    var censusData = json;
    console.log(censusData.areas);
});

这会将所有状态记录到控制台作为对象,因此我尝试使用以下内容进入另一个级别:

$.getJSON("../data/census.json", function (json) {
    var censusData = json;
    console.log(censusData.areas.UnitedStates);
});

但这会返回“undefined”。我怎样才能深入了解另一个级别?理想情况下,我想访问特定年份的每个州人口,然后将其分配给我稍后可以使用的变量。

5 个答案:

答案 0 :(得分:0)

目前没有二级,因为州名和它们的属性之间有逗号而不是冒号:

"Alaska", [{
   "STATEORREGION":"Alaska",
   "1910POPULATION":64356,
   ...

这使得值“Alaska”和列表中的属性字典相邻元素。看来你想要的是:

"Alaska": [{
   "STATEORREGION":"Alaska",
   "1910POPULATION":64356,
   ...

答案 1 :(得分:0)

这里的模式是在每个字符串名称之后我们有一组相关信息: 所以你可以把它转换成像

这样的对象
var _result = {};
   for ( var i=0;i<arr.length/2;i++){
      _result[arr[i]] = arr[i+1];
   }

然后您可以通过字符串名称直接访问这些信息

像这样

censusData.areas.UnitedStates

将在此转换后返回一个数组

答案 2 :(得分:0)

你的json数据无效试试这个,

{"areas" : [
  "UnitedStates", [{
    "STATEORREGION":"United States",
    "1910POPULATION":92228531,
    "1920POPULATION":106021568,
    "1930POPULATION":123202660,
    "1940POPULATION":132165129,
    "1950POPULATION":151325798,
    "1960POPULATION":179323175,
    "1970POPULATION":203211926,
    "1980POPULATION":226545805,
    "1990POPULATION":248709873,
    "2000POPULATION":281421906,
    "2010POPULATION":308745538,
    "1910CHANGE":21,
    "1920CHANGE":15,
    "1930CHANGE":16.2,
    "1940CHANGE":7.3,
    "1950CHANGE":14.5,
    "1960CHANGE":18.5,
    "1970CHANGE":13.3,
    "1980CHANGE":11.5,
    "1990CHANGE":9.8,
    "2000CHANGE":13.2,
    "2010CHANGE":9.7
  }],
  "Alabama", [{
    "STATEORREGION":"Alabama",
    "1910POPULATION":2138093,
    "1920POPULATION":2348174,
    "1930POPULATION":2646248,
    "1940POPULATION":2832961,
    "1950POPULATION":3061743,
    "1960POPULATION":3266740,
    "1970POPULATION":3444165,
    "1980POPULATION":3893888,
    "1990POPULATION":4040587,
    "2000POPULATION":4447100,
    "2010POPULATION":4779736,
    "1910CHANGE":16.9,
    "1920CHANGE":9.8,
    "1930CHANGE":12.7,
    "1940CHANGE":7.1,
    "1950CHANGE":8.1,
    "1960CHANGE":6.7,
    "1970CHANGE":5.4,
    "1980CHANGE":13.1,
    "1990CHANGE":3.8,
    "2000CHANGE":10.1,
    "2010CHANGE":7.5
  }],
  "Alaska", [{
    "STATEORREGION":"Alaska",
    "1910POPULATION":64356,
    "1920POPULATION":55036,
    "1930POPULATION":59278,
    "1940POPULATION":72524,
    "1950POPULATION":128643,
    "1960POPULATION":226167,
    "1970POPULATION":300382,
    "1980POPULATION":401851,
    "1990POPULATION":550043,
    "2000POPULATION":626932,
    "2010POPULATION":710231,
    "1910CHANGE":1.2,
    "1920CHANGE":-14.5,
    "1930CHANGE":7.7,
    "1940CHANGE":22.3,
    "1950CHANGE":77.4,
    "1960CHANGE":75.8,
    "1970CHANGE":32.8,
    "1980CHANGE":33.8,
    "1990CHANGE":36.9,
    "2000CHANGE":14,
    "2010CHANGE":13.3
  }]
]
}

答案 3 :(得分:0)

因此我可以通过将JSON的结构更改为以下内容来访问数据:

{"areas" : [ [{ "STATEORREGION":"United States", "POPULATION1910":92228531, "1920POPULATION":106021568, "1930POPULATION":123202660, "1940POPULATION":132165129, "1950POPULATION":151325798, "1960POPULATION":179323175, "1970POPULATION":203211926, "1980POPULATION":226545805, "1990POPULATION":248709873, "2000POPULATION":281421906, "2010POPULATION":308745538, "1910CHANGE":21, "1920CHANGE":15, "1930CHANGE":16.2, "1940CHANGE":7.3, "1950CHANGE":14.5, "1960CHANGE":18.5, "1970CHANGE":13.3, "1980CHANGE":11.5, "1990CHANGE":9.8, "2000CHANGE":13.2, "2010CHANGE":9.7 }], [{ "STATEORREGION":"Alabama", "1910POPULATION":2138093, "1920POPULATION":2348174, "1930POPULATION":2646248, "1940POPULATION":2832961, "1950POPULATION":3061743, "1960POPULATION":3266740, "1970POPULATION":3444165, "1980POPULATION":3893888, "1990POPULATION":4040587, "2000POPULATION":4447100, "2010POPULATION":4779736, "1910CHANGE":16.9, "1920CHANGE":9.8, "1930CHANGE":12.7, "1940CHANGE":7.1, "1950CHANGE":8.1, "1960CHANGE":6.7, "1970CHANGE":5.4, "1980CHANGE":13.1, "1990CHANGE":3.8, "2000CHANGE":10.1, "2010CHANGE":7.5 }], [{ "STATEORREGION":"Alaska", "1910POPULATION":64356, "1920POPULATION":55036, "1930POPULATION":59278, "1940POPULATION":72524, "1950POPULATION":128643, "1960POPULATION":226167, "1970POPULATION":300382, "1980POPULATION":401851, "1990POPULATION":550043, "2000POPULATION":626932, "2010POPULATION":710231, "1910CHANGE":1.2, "1920CHANGE":-14.5, "1930CHANGE":7.7, "1940CHANGE":22.3, "1950CHANGE":77.4, "1960CHANGE":75.8, "1970CHANGE":32.8, "1980CHANGE":33.8, "1990CHANGE":36.9, "2000CHANGE":14, "2010CHANGE":13.3 }] ] }

然后我用了:

$.getJSON( "../data/census.json", function( json ) { var censusData = json; console.log(censusData.areas[0][0].POPULATION1910); });

另请注意,我必须将1910POPULATION更改为POPULATION1910才能访问它而不会出现错误。可能有一种更好的方法来改变每个州的每个关键名称。

当我想访问下一个状态时,我刚刚将censusData[0][0]更改为censusData[1][0],依旧等等。

答案 4 :(得分:-1)

字符串"United States"是列表中的第一个元素。

美国的数据位于censusData.areas[1]。 JSON的结构看起来有点奇怪。