【问题标题】:How to access nested value from JSON Instagram API with Javascript如何使用 Javascript 从 JSON Instagram API 访问嵌套值
【发布时间】:2014-01-08 08:21:30
【问题描述】:

我正在尝试从 https://github.com/bigflannel/bigflannel-Instafeed 修改脚本以访问网站中的 Instagram 照片。但它不包括显示照片 cmets 的功能。所以,我试图修改,但它返回未定义的值。该脚本使用 javascript 从 API 访问数据。 示例:

[
{
    "attribution": null,
    "tags": [

    ],
    "type": "image",
    "location": null,
    "comments": {
        "count": 2,
        "data": [
            {
                "created_time": "1389168592",
                "text": "Beautiful bridge!",
                "from": {
                    "username": "realwahyuputra",
                    "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg",
                    "id": "180213154",
                    "full_name": "realwahyuputra"
                },
                "id": "628714182443349004"
            },
            {
                "created_time": "1389168601",
                "text": "also good views",
                "from": {
                    "username": "realwahyuputra",
                    "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg",
                    "id": "180213154",
                    "full_name": "realwahyuputra"
                },
                "id": "628714254652486672"
            }
        ]
    },
    "filter": "Hefe",
    "created_time": "1350749506",
    "link": "http:\/\/instagram.com\/p\/RAqdlGyTSc\/",
    "likes": {
        "count": 0,
        "data": [

        ]
    },
    "images": {
        "low_resolution": {
            "url": "http:\/\/distilleryimage0.s3.amazonaws.com\/d87203101ad011e297b922000a1fa527_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http:\/\/distilleryimage0.s3.amazonaws.com\/d87203101ad011e297b922000a1fa527_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http:\/\/distilleryimage0.s3.amazonaws.com\/d87203101ad011e297b922000a1fa527_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "users_in_photo": [

    ],
    "caption": {
        "created_time": "1350749545",
        "text": "From the office",
        "from": {
            "username": "bigflannel",
            "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg",
            "id": "240129684",
            "full_name": "Mike Hartley"
        },
        "id": "306431853609956969"
    },
    "user_has_liked": false,
    "id": "306431525321782428_240129684",
    "user": {
        "username": "bigflannel",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg",
        "full_name": "Mike Hartley",
        "bio": "",
        "id": "240129684"
    }
}];

这是从该 JSON 访问数据的函数之一:

function imageCaptionText(timestamp) {
var text = 'Filter: ' + imageData[imageCount].filter + '<br />'
if (imageData[imageCount].caption != null) {
    text = text + 'Caption: ' +  imageData[imageCount].caption.text + '<br />';
}
if (imageData[imageCount].likes.count > 0) {
    text = text + 'Likes: ' + imageData[imageCount].likes.count + '<br />';
}
if (imageData[imageCount].comments.count > 0) {
    text = text + 'Comments: ' + imageData[imageCount].comments.count + '<br />';
}
if (imageData[imageCount].comments.data != null) {
    text = text + 'Comments Data: ' + imageData[imageCount].comments.data.text + '<br />';
}
if (imageData[imageCount].location != null) {
    text = text + 'Location: ' + imageData[imageCount].location + '<br />';
}
var date = new Date(1000*timestamp);
text = text + 'Date: ' + date.toLocaleString() + '<br />';
text = text + '<a href="' + imageData[imageCount].link + '">On Instagram</a><br />';
return text; }

除此代码返回 undefined 值外,一切正常(我正在尝试创建它以访问 cmets 数据)

    if (imageData[imageCount].comments.data != null) {
    text = text + 'Comments Data: ' + imageData[imageCount].comments.data.text + '<br />';
}

如何让它发挥作用?任何帮助,将不胜感激。谢谢:)

【问题讨论】:

    标签: javascript json instagram


    【解决方案1】:

    cmets.data 是一个数组,实际文本将位于imageData[imageCount].comments.data[commentCount].text,因此您必须执行以下操作:

    if (imageData[imageCount].comments.data != null) {
        text = 'Comments Data:<br />';
        imageData[imageCount].comments.data.forEach(function(comment){
            text += comment.from.username + ': ' + comment.text + '<br />';
        });
    }
    

    【讨论】:

    • 像魅力一样工作!谢谢克里斯拉克 :)
    • 你好 krisrak,把它写成 PHP 代码怎么样?
    猜你喜欢
    • 2021-04-21
    • 2018-07-06
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多