gpt4 book ai didi

javascript - AJAX 请求从包含对象的数组中提取信息

转载 作者:行者123 更新时间:2023-12-03 07:46:11 24 4
gpt4 key购买 nike

我需要一些帮助来处理我正在处理的 ajax 请求。目前我将此作为我的 ajax 调用

$.ajax({
type: 'GET',
url: 'https://teamtreehouse.com/garrettsanderson.json',
dataType: 'json',
success: function(treeHouseDatas) {
var data = JSON.stringify(treeHouseDatas)
$.each(treeHouseDatas, function(i, treeHouseData) {
$('#treehouse-info').append('<p>' + treeHouseData + '</p>');
console.log(treeHouseData);
});
}
});

我在控制台中得到的响应如下:

Garrett Sanderson

undefined

garrettsanderson

undefined

https://teamtreehouse.com/garrettsanderson

undefined

https://uploads.teamtreehouse.com/production/profile- photos/194972/avatar_about-me-photo.png

undefined

ed16b4c7fd1211bd25b61bb9efd2d3cf

undefined

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]0: Object1: Object2: Object3: Object4: Object5: Object6: Object7: Object8: Object9: Object10: Object11: Object12: Object13: Object14: Object15: Object16: Object17: Object18: Object19: Object20: Object21: Object22: Object23: Object24: Object25: Object26: Object27: Object28: Object29: Object30: Object31: Object32: Object33: Object34: Object35: Object36: Object37: Object38: Object39: Object40: Object41: Object42: Object43: Object44: Object45: Object46: Object47: Object48: Object49: Object50: Object51: Object52: Object53: Object54: Object55: Object56: Object57: Object58: Object59: Object60: Object61: Object62: Object63: Object64: Object65: Object66: Object67: Object68: Object69: Object70: Object71: Object72: Object73: Object74: Object75: Object76: Object77: Object78: Object79: Object80: Object81: Object82: Object83: Objectlength: 84__proto__: Array[0]

新手

实际的 JSON 文件如下所示

    {
name: "Garrett Sanderson",
profile_name: "garrettsanderson",
profile_url: "https://teamtreehouse.com/garrettsanderson",
gravatar_url: "https://uploads.teamtreehouse.com/production/profile-photos/194972/avatar_about-me-photo.png",
gravatar_hash: "ed16b4c7fd1211bd25b61bb9efd2d3cf",
badges: [],
points: {}
}

如何访问徽章数组和点对象?截至目前,我只是得到了对象,对象吐出。

任何帮助将不胜感激!

谢谢!

最佳答案

How do I access the badges array

您可以在每个循环中定位徽章数组,然后访问内部的对象,现在您可以定位对象的特定键:

$.ajax({
type: 'GET',
url: 'https://teamtreehouse.com/garrettsanderson.json',
dataType: 'json',
success: function(treeHouseDatas) {
$.each(treeHouseDatas.badges, function(i, tree) {
$('body').append('<p style="border-bottom:solid 1px #c4c4c4;"><strong>' +
tree.id +':::::</strong>' +
tree.url + '</p>');
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

and the points object?

console.log(treeHouseDatas.points);

关于javascript - AJAX 请求从包含对象的数组中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com