1 回答

TA贡献1872条经验 获得超3个赞
要解析数据,您需要了解 API 的数据结构。
{
items: [
{
post_count,
score,
user: {
accept_rate,
display_name,
link, // To profile page
profile_image, // A URL
reputation,
user_id, // A Number
user_type // E.G. "moderator"
}
},
// More of the sample object above.
],
quota_max,
quote_remaining
}
此代码段有一些更改,您可以通过查看上面的结构来理解这些更改。
<html>
<head>
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
var url="https://api.stackexchange.com/2.2/tags/jquery/top-answerers/all_time?site=stackoverflow";
$.getJSON(url,function(result){
console.log(result)
// loop through results.items NOT results
$.each(result.items, function(i, data){
var user_id=data.user.user_id; // NOT data.user_id
var profile_image=data.user.profile_image; // NOT data.profile_image
var post_count=data.post_count;
var score=data.score;
$("#listview").append(`
<table>
<tr>
<td><img src="${profile_image}"></td>
<td>
UserID: ${user_id}<br/>
Post Count: ${post_count}<br/>
Score: ${score}
</td>
</tr>
</table>
`);
});
});
</script>
</head>
<body>
<div id="listview">
</div>
</body>
</html>
添加回答
举报