-1

我的 javascript 代码返回了一个 json 文件。该文件如下所示:

{
    "data": [
        {
            "id": "594984240522886",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Ducks",
            "description": "ducks",
            "link": "http://www.facebook.com/album.php?fbid=594984240522886&id=593959083958735&aid=1073741834",
            "cover_photo": "594984260522884",
            "count": 4,
            "type": "normal",
            "created_time": "2013-06-13T15:12:22+0000",
            "updated_time": "2013-06-13T15:12:40+0000",
            "can_upload": false
        },
        {
            "id": "593963787291598",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Profile Pictures",
            "link": "http://www.facebook.com/album.php?fbid=593963787291598&id=593959083958735&aid=1073741832",
            "cover_photo": "593963797291597",
            "count": 1,
            "type": "profile",
            "created_time": "2013-06-11T16:52:29+0000",
            "updated_time": "2013-06-11T16:52:31+0000",
            "can_upload": false
        },
        {
            "id": "593963467291630",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Goats",
            "description": "goats",
            "link": "http://www.facebook.com/album.php?fbid=593963467291630&id=593959083958735&aid=1073741831",
            "cover_photo": "593963477291629",
            "count": 7,
            "type": "normal",
            "created_time": "2013-06-11T16:51:56+0000",
            "updated_time": "2013-06-11T16:52:02+0000",
            "can_upload": false
        },
        {
            "id": "593962700625040",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Dogs",
            "description": "dogs",
            "link": "http://www.facebook.com/album.php?fbid=593962700625040&id=593959083958735&aid=1073741830",
            "cover_photo": "593962710625039",
            "count": 10,
            "type": "normal",
            "created_time": "2013-06-11T16:50:27+0000",
            "updated_time": "2013-06-11T16:50:37+0000",
            "can_upload": false
        },
        {
            "id": "593961937291783",
            "from": {
                "id": "593959083958735",
                "category": "Community",
                "name": "Decoc"
            },
            "name": "Cows",
            "description": "Cows",
            "link": "http://www.facebook.com/album.php?fbid=593961937291783&id=593959083958735&aid=1073741829",
            "cover_photo": "593961983958445",
            "count": 5,
            "type": "normal",
            "created_time": "2013-06-11T16:48:26+0000",
            "updated_time": "2013-06-11T16:49:32+0000",
            "can_upload": false
        }
    ],
    "paging": {
        "cursors": {
            "after": "NTkzOTYxOTM3MjkxNzgz",
            "before": "NTk0OTg0MjQwNTIyODg2"
        }
    }
}

我想在“数据”内部循环,看看有多少不同的数据元素存在(你看到每个元素都有一个 id 、 from 、 name 、 description ..)。我怎么能用javascript做到这一点?

4

1 回答 1

2

您可以尝试以下代码:

for(i=0;json.data.length;i++){
    var element = json.data[i];
}

或者以其他方式:

for (i in json.data) {
    if (json.data.hasOwnProperty(i)) {
        var element = json.data[i];
    }
}
于 2013-06-17T16:29:43.813 回答