我試着從陣列中獲取價值:獲得從嵌套數組(JSON)值
[
{
"id": "5899aaa321e01b8b050041cb",
"name": "John Doe",
"picture": {"small": "https://cdn.image.com/1234"},
"age": 28,
"location": {"city": "London"},
"following": 1,
"resources": {"band": "http://image/music"},
"top_works": [
{
"points": 20,
"portfolio": {
"facebook_id": "1e691681472",
"picture": {"small": "https://cdn.image.com/1234"}
}
},
{
"points": 10,
"portfolio": {
"facebook_id": "1e6916r17ry",
"picture": {"small": "https://cdn.image.com/1234"}
}
}
]
}
]
$(document).ready(function() {
$.ajax({
type: 'GET',
url: '/api/url/123',
data: { get_param: 'value' },
dataType: 'json',
success: function (data) {
$.each(data, function (index, element) {
$('#api').append('<div>'
+ ' <div><h3>' + element.name + '</h3></div>'
+ '<div>' + element.top_works[2].portfolio.picture.small + '></div>');
});
}
});
});
我可以毫無問題地閱讀所有的,無論是「top_works,因爲我需要具體數目[X],我需要一個循環。
在某些情況下,如果top_works爲空,我得到一個「控制檯未定義」,我需要表現出的白色空間,如果該元素一點兒也不exsist。
有人能幫助我?
什麼是輸入什麼輸出?你有什麼嘗試? –
陣列格式不正確。 – Mamun