从Json中获取喜欢的用户名列表

时间:2012-10-06 23:23:48

标签: json

如果我在下面有以下代码,我将如何显示喜欢的用户名?目前我使用photo.images.low_resolution.url向我展示低分辨率图像的urk,但我无法弄清楚喜欢的部分。

{
    "data": [{
        "location": {
            "id": "833",
            "latitude": 37.77956816727314,
            "longitude": -122.41387367248539,
            "name": "Civic Center BART"
        },
        "comments": {
            "count": 16,
            "data": [ ... ]
        },
        "caption": null,
        "link": "http://instagr.am/p/BXsFz/",
        "likes": {
            "count": 190,
            "data": [{
                "username": "shayne",
                "full_name": "Shayne Sweeney",
                "id": "20",
                "profile_picture": "..."
            }, {...subset of likers...}]
        },
        "created_time": "1296748524",
        "images": {
            "low_resolution": {
                "url": "http://distillery.s3.amazonaws.com/media/2011/02/03/efc502667a554329b52d9a6bab35b24a_6.jpg",
                "width": 306,
                "height": 306
            },
            "thumbnail": {
                "url": "http://distillery.s3.amazonaws.com/media/2011/02/03/efc502667a554329b52d9a6bab35b24a_5.jpg",
                "width": 150,
                "height": 150
            },
            "standard_resolution": {
                "url": "http://distillery.s3.amazonaws.com/media/2011/02/03/efc502667a554329b52d9a6bab35b24a_7.jpg",
                "width": 612,
                "height": 612
            }
        },
        "type": "image",
        "filter": "Earlybird",
        "tags": [],
        "id": "22987123",
        "user": {
            "username": "kevin",
            "full_name": "Kevin S",
            "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
            "id": "3"
        }
    },
    ...]
}

2 个答案:

答案 0 :(得分:1)

jsFiddle。请注意,第二个条目没有数据。

答案 1 :(得分:0)

请:

            //photo = your object
            //Make sure to remove empty objects({...}) as in your code example.

            for (var i = 0; i < photo.data.length; i++) {
                    var photoData = photo.data[i];
                    for (var x = 0; x < photoData.likes.data.length; x++) {
                        //Do what ever you want with the like's user...
                        var likeData = photoData.likes.data[x];
                        alert(likeData.username);
                    }
                }

下次使用console.log浏览对象: enter image description here