2015-06-25 61 views
1

我是新來的JSON,我試圖返回我的Facebook相冊的列表:解析Facebook的圖形API JSON的HTML

從Facebook
$(document).ready(function() { 
    $.getJSON("https://graph.facebook.com/me/photos?fields=id,name&access_token=MY TOKEN", function (data) { 
     var items = []; 
     $.each(data, function (key, val) { 
      items.push("<li id='" + key + "'>" + val + "</li>"); 
     }); 

     $("<ul/>", { 
      "class": "my-new-list", 
      html: items.join("") 
     }).appendTo(".results"); 
    }); 
}); 

JSON:

{ 
    "data": [ 
     { 
     "id": "10150589771916817", 
     "name": "Mobile Uploads", 
     "created_time": "2012-03-03T14:47:48+0000" 
     }, 
     { 
     "id": "41633726816", 
     "name": "Old pics", 
     "created_time": "2008-08-18T21:44:29+0000" 
     } 
    ], 
    "paging": { 
     "cursors": { 
     "after": "NDE2MzM3MjY4MTY=", 
     "before": "MTAxNTA1ODk3NzE5MTY4MTc=" 
     } 
    } 
} 

[object Object]被退回每個項目在我的HTML。我究竟做錯了什麼?

回答

1

首先,響應中有一個名爲data的數組,因此您需要循環使用data.data

其次,在你的例子中key將是迭代的整數索引,而value將是數組中的整個對象。您需要明確地訪問該數組的屬性。試試這個:

// Your retrieved JSON data... 
 
var data = { 
 
    "data": [{ 
 
     "id": "10150589771916817", 
 
     "name": "Mobile Uploads", 
 
     "created_time": "2012-03-03T14:47:48+0000" 
 
    }, { 
 
     "id": "41633726816", 
 
     "name": "Old pics", 
 
     "created_time": "2008-08-18T21:44:29+0000" 
 
    }], 
 
    "paging": { 
 
     "cursors": { 
 
      "after": "NDE2MzM3MjY4MTY=", 
 
      "before": "MTAxNTA1ODk3NzE5MTY4MTc=" 
 
     } 
 
    } 
 
} 
 

 
// inside your AJAX request callback... 
 
var items = []; 
 
$.each(data.data, function (i, obj) { 
 
    items.push('<li id="' + obj.id + '">' + obj.name + '</li>'); 
 
}); 
 

 
$('<ul/>', { 'class': 'my-new-list' }).append(items).appendTo('.results');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="results"></div>

+0

謝謝。我知道我在閱讀json格式時會出錯。現在我明白了很多。謝謝! –

+0

沒問題,很樂意幫忙 –

+0

我如何在JSON中鑽取另一個級別?說出數據中是否有圖像:「data」:[「images」:[]]? –

1

響應包含一個名爲數組,盧普認爲數據數組,然後你可以搶在第二循環的關鍵和Val。

這樣做,您可以訪問密鑰和val。

var fbResponse = { 
 
    "data": [{ 
 
     "id": "10150589771916817", 
 
      "name": "Mobile Uploads", 
 
      "created_time": "2012-03-03T14:47:48+0000" 
 
    }, { 
 
     "id": "41633726816", 
 
      "name": "Old pics", 
 
      "created_time": "2008-08-18T21:44:29+0000" 
 
    }], 
 
     "paging": { 
 
     "cursors": { 
 
      "after": "NDE2MzM3MjY4MTY=", 
 
       "before": "MTAxNTA1ODk3NzE5MTY4MTc=" 
 
     } 
 
    } 
 
}; 
 

 

 
$.each(fbResponse.data, function (index, item) { 
 
    $.each(item, function (key, val) { 
 
     alert("Index=" + index + ", key=" + key + ", val=" + val); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

1

使用jQuery當你循環數組你得到指數

$.each([ 10, 20 ], function(index, value) { 
    alert(index + ": " + value); 
}); 

輸出:

0: 10 
0: 20 

如果你迭代集合你關鍵

var obj = { 
    "key 1": "value 1", 
    "key 2": "value 2" 
}; 
$.each(obj, function(key, value) { 
    alert(key + ": " + value); 
}) 

輸出:

key 1: value 1 
key 2: value 2