访问没有名称的json对象

时间:2014-11-19 07:23:54

标签: jquery ajax json getjson

我正在努力访问下面的这个json代码。我之前看到的每个json值都是这样的:

 var hold = { name: 'alex', job: 'realtor'}

因此,要访问该名称,我会执行类似hold.name的操作。但我的json数据看起来像这样

[
[
        {
            "count": "80",
            "countTxt":"Buyer logins",
            "participantsTxt":"Total for all participating member companies:",
            "participantCount":"523"
        },
        {
            "count": "233",
            "countTxt":"Searches",
            "participantsTxt":"Total for all participating member companies:",
            "participantCount":"628"
        },
        {
            "count": "533",
            "countTxt":"Views of our member pages",
            "participantsTxt":"Total for all participating member companies:",
            "participantCount":"2,365"
        }
    ],


[
        {
            "count": "80",
            "countTxt":"Total vists",
            "participantsTxt":"Total for all participating member companies:",
            "participantCount":"412"
        },
        {
            "count": "53",
            "countTxt":"Unique visitors",
            "participantsTxt":"Total for all participating member companies:",
            "participantCount":"731"
        },
        {
            "count": "1:12 min",
            "countTxt":"Total time spent on page",
            "participantsTxt":"Total for all participating member companies:",
            "participantCount":"784.2 mins"
        }
    ]

数据是嵌套的,没有名称,所以我不知道如何访问。我知道这个问题似乎有点基础但我还没有看到任何解析代码看起来像这样的例子。它总是一个比上面更简单的例子。我的.getjson代码看起来像这样......

var requestURL = 'assets/js/stats.json';
$.getJSON(requestURL, function(data){
        $.each(data, function(key, value) {
            $('body').append($('<div>' + data[0].name, {

            }));
            console.log(data);
        });

    });

我的控制台正在返回两个看起来像这样的对象

Array[3], Array[3]] Array[3]: 
Array[3] [Array[3], Array[3]] 

所以我知道它正在获取数据我只需要能够选择我想要的东西将它附加到DOM。我尝试了十种不同的变化,但没有任何效果。请帮忙!?

1 个答案:

答案 0 :(得分:1)

您只需要在第一次迭代中再次迭代一次。您有一个包含对象数组的数组。所以试试这样的事情

http://jsfiddle.net/mnth1xe1/5/

$.each(data, function(key, val) {
    $.each(val, function(key, value) {

        console.log(value.countTxt);
    });

});