如何使用jquery .each()遍历解析的JSON数组

时间:2018-10-28 12:44:41

标签: javascript jquery arrays json

我正在解析以下数据,然后循环浏览以尝试获取每个状态ID和名称。

{
    "billing": {
        "ACT": "Australian Capital Territory",
        "NSW": "New South Wales",
        "NT": "Northern Territory",
        "QLD": "Queensland",
        "SA": "South Australia",
        "TAS": "Tasmania",
        "VIC": "Victoria",
        "WA": "Western Australia"
    },
    "shipping": {
        "ACT": "Australian Capital Territory",
        "NSW": "New South Wales",
        "NT": "Northern Territory",
        "QLD": "Queensland",
        "SA": "South Australia",
        "TAS": "Tasmania",
        "VIC": "Victoria",
        "WA": "Western Australia"
    }
}

data = '{"billing":{"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria","WA":"Western Australia"},"shipping":{"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria","WA":"Western Australia"}}';

data = jQuery.parseJSON( data );
billingData = data.billing;

$(billingData).each( function( key, value ) {
    console.log( key + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我期望控制台遍历每个状态ID和标签,但是我得到它们的键为0,值作为对象,我也尝试遍历输出的对象(包含原始{{ 1}})。

我还尝试遍历.each

3 个答案:

答案 0 :(得分:2)

您需要使用jQuery.each()而不是.each()来完成这项工作。

.each()遍历jquery元素,而jQuery.each()遍历数组或对象。

data = '{"billing":{"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria","WA":"Western Australia"},"shipping":{"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria","WA":"Western Australia"}}';

data = jQuery.parseJSON(data);
billingData = data.billing;

$.each(billingData, function(key, value) {
 console.log(key +": "+ value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:1)

billingData不是数组。这是一个对象。 jQuery each可以让您像遍历数组一样遍历对象,也可以只使用对象方法:

data = '{"billing":{"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria","WA":"Western Australia"},"shipping":{"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria","WA":"Western Australia"}}';

data = jQuery.parseJSON(data);
billingData = data.billing;

Object.keys(billingData).forEach(function(key) {
  console.log(key + ": " + billingData[key])
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 2 :(得分:-1)

每种都有两种类型:

$。each(array,function(key,value){

});

$('。element')。each(function(){

});