2014-01-08 46 views
1

我試圖從https://github.com/bigflannel/bigflannel-Instafeed修改腳本來訪問網站中的Instagram照片。但它不包括顯示照片評論的功能。所以,即時嘗試修改,但它返回未定義的值。該腳本使用JavaScript從API訪問數據。 例子:如何使用JavaScript從JSON Instagram 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; } 

一切順利,除了這個代碼返回未定義值(我試圖創建此訪問評論數據)

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

如何使它工作?任何幫助,將不勝感激。謝謝:)

回答

2

comments.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 />'; 
    }); 
} 
+0

作品般的魅力! 謝謝krisrak :) – cutez7boyz

+0

你好krisrak,如何寫下來到PHP代碼? – cutez7boyz