这是我的代码,它可以正常使用
(function ($) {
$('input').on('click', function () {
$('.table ul').remove();
$("#guildwars2").val();
// selection made
var zip = $('#guildwars2 option:selected').text();
// guild wars 2 guild api key
var apkey = ('?access_token=...');
$.getJSON("https://api.guildwars2.com/v2/guild/645BD706-FC0B-41B5-921C-ED07E6A7D8FC/" + zip + apkey + "", function (data) {
var items = [];
$.each(data, function (key, val) {
if (zip == 'members'){
items.push('<li id=" ' + key + '"=>' + val.name + " - " + val.rank + '</li>');
} else if (zip == 'ranks'){
items.push('<li id="' + key + '"><br><span class="imagepng">' + val.icon + ' - ' + val.id + '</li>');
} else if (zip == 'log'){
items.push('<li id="' + key + '"><br><span class="id">' + val.time + '</span><br><span class="time">' + val.type + ' By ' + val.user + '</span></li>');
}
});
if (items.length < 1) {
items.push('<li>No results , try again!</li>');
}
$ul = $('<ul />').appendTo('.table');
$ul.append(items);
});
});
}(jQuery));
但是如何获取所有数据而不是val.name或val.rank
我试过了
(function ($) {
$('input').on('click', function () {
$('.table ul').remove();
$("#guildwars2").val();
// selection made
var zip = $('#guildwars2 option:selected').text();
// guild wars 2 guild api key
var apkey = ('?access_token=...');
$.getJSON("https://api.guildwars2.com/v2/guild/645BD706-FC0B-41B5-921C-ED07E6A7D8FC/" + zip + apkey + "", function (data) {
var items = [];
$.each(data, function (key, val) {
items.push('<li id="' + key + '"><br><span class="id">' + val + '</span></li>');
});
if (items.length < 1) {
items.push('<li>No results , try again!</li>');
}
$ul = $('<ul />').appendTo('.table');
$ul.append(items);
});
});
}(jQuery));
但这只返回对象,所有值的对象
答案 0 :(得分:0)
您需要访问数组中返回的对象:
var apkey = ('?access_token=...');
$("#guildwars2").on("change",function() {
var zip = $('option:selected',this).text(),
$.getJSON("https://api.guildwars2.com/v2/guild/645BD706-FC0B-41B5-921C-ED07E6A7D8FC/" + zip + apkey, function (data) {
$.each(data, function(index, obj) {
items.push('<ul id="' + index + '">')
for (o in obj) {
items.push('<li>' + o + ":"+obj[o] + '</li>');
}
items.push('</ul>');
});
$("#container").html(items.length>0?items.join(""):"Nothing found")
});
});
示例:
data = [{
"name": "Scotty The Engineer.1560",
"rank": "Leader",
"joined": "2013-05-27T20:33:01.000Z"
},
{
"name": "Srg Farr.3045",
"rank": "Leader",
"joined": "2015-11-24T19:01:29.000Z"
},
{
"name": "sassy.8241",
"rank": "Leader",
"joined": null
},
{
"name": "nytri.3401",
"rank": "Officer",
"joined": "2015-08-12T17:13:53.000Z"
},
{
"name": "Demonic Angel.3496",
"rank": "Donor",
"joined": "2015-11-27T00:26:45.000Z"
},
{
"id": 3412,
"time": "2016-09-11T23:03:02.000Z",
"type": "invite_declined",
"user": "magic magician master.8614",
"declined_by": "magic magician master.8614"
},
{
"id": 3225,
"time": "2016-05-16T22:24:46.000Z",
"type": "rank_change",
"user": "Srg Farr.3045",
"changed_by": "sassy.8241",
"old_rank": "Co Leader",
"new_rank": "Leader"
},
{
"id": 3224,
"time": "2016-05-16T22:24:37.000Z",
"type": "rank_change",
"user": "Scotty The Engineer.1560",
"changed_by": "sassy.8241",
"old_rank": "Co Leader",
"new_rank": "Leader"
},
{
"id": 2967,
"time": "2015-12-31T21:55:27.000Z",
"type": "motd",
"user": "sassy.8241",
"motd": "Welcome friends to the family "
},
{
"id": 2948,
"time": "2015-12-20T13:01:13.000Z",
"type": "joined",
"user": "Demonic Angel.3496"
},
{
"id": 2909,
"time": "2015-11-28T17:53:54.000Z",
"type": "motd",
"user": "sassy.8241",
"motd": "Welcome To Our Friendly Guild, If you need any help in any area please ask in chat and i am sure someone will help you.\nalso if you want to do dungeon runs ect please ask an officer and we can arrange a full guild run.\nWelcome our newest member sarge :P\n\nMrs Tech is putting up Christmas decorations tonight will be on asap"
},
{
"id": 2904,
"time": "2015-11-27T09:56:40.000Z",
"type": "rank_change",
"user": "Srg Farr.3045",
"changed_by": "sassy.8241",
"old_rank": "Leader",
"new_rank": "Co Leader"
},
{
"id": 2903,
"time": "2015-11-27T09:56:22.000Z",
"type": "rank_change",
"user": "Srg Farr.3045",
"changed_by": "sassy.8241",
"old_rank": "Co Leader",
"new_rank": "Leader"
},
{
"id": 2901,
"time": "2015-11-27T00:26:46.000Z",
"type": "invited",
"user": "Demonic Angel.3496",
"invited_by": "Srg Farr.3045"
},
{
"id": 2886,
"time": "2015-11-24T19:01:55.000Z",
"type": "rank_change",
"user": "Srg Farr.3045",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Donor",
"new_rank": "Co Leader"
},
{
"id": 2885,
"time": "2015-11-24T19:01:30.000Z",
"type": "joined",
"user": "Srg Farr.3045"
},
{
"id": 2883,
"time": "2015-11-24T19:01:05.000Z",
"type": "invited",
"user": "Srg Farr.3045",
"invited_by": "Scotty The Engineer.1560"
},
{
"id": 2882,
"time": "2015-11-24T18:30:41.000Z",
"type": "kick",
"user": "Srg Farr.3045",
"kicked_by": "Srg Farr.3045"
},
{
"id": 2880,
"time": "2015-11-23T23:16:20.000Z",
"type": "kick",
"user": "Amsera.8179",
"kicked_by": "sassy.8241"
},
{
"id": 2879,
"time": "2015-11-23T23:16:17.000Z",
"type": "kick",
"user": "Devishly Shell.6102",
"kicked_by": "sassy.8241"
},
{
"id": 2878,
"time": "2015-11-23T22:26:12.000Z",
"type": "rank_change",
"user": "Devishly Shell.6102",
"changed_by": "sassy.8241",
"old_rank": "Donor",
"new_rank": "Member"
},
{
"id": 2877,
"time": "2015-11-23T22:26:09.000Z",
"type": "rank_change",
"user": "Amsera.8179",
"changed_by": "sassy.8241",
"old_rank": "Donor",
"new_rank": "Member"
},
{
"id": 2876,
"time": "2015-11-23T22:25:24.000Z",
"type": "rank_change",
"user": "Srg Farr.3045",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Officer",
"new_rank": "Co Leader"
},
{
"id": 2858,
"time": "2015-11-15T01:28:56.000Z",
"type": "rank_change",
"user": "Srg Farr.3045",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Donor",
"new_rank": "Officer"
},
{
"id": 2857,
"time": "2015-11-15T01:28:34.000Z",
"type": "motd",
"user": "Scotty The Engineer.1560",
"motd": "Welcome To Our Friendly Guild, If you need any help in any area please ask in chat and i am sure someone will help you.\n\nalso if you want to do dungeon runs ect please ask an officer and we can arrange a full guild run.\n\n\nWelcome our newest member sarge :P"
},
{
"id": 2855,
"time": "2015-11-14T22:56:47.000Z",
"type": "joined",
"user": "Srg Farr.3045"
},
{
"id": 2853,
"time": "2015-11-14T22:56:13.000Z",
"type": "invited",
"user": "Srg Farr.3045",
"invited_by": "sassy.8241"
},
{
"id": 2719,
"time": "2015-08-29T13:33:44.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2717,
"time": "2015-08-29T00:44:26.000Z",
"type": "stash",
"user": "nytri.3401",
"operation": "deposit",
"item_id": 39495,
"count": 1,
"coins": 0
},
{
"id": 2714,
"time": "2015-08-29T00:44:17.000Z",
"type": "stash",
"user": "nytri.3401",
"operation": "deposit",
"item_id": 20601,
"count": 1,
"coins": 0
},
{
"id": 2711,
"time": "2015-08-29T00:44:14.000Z",
"type": "stash",
"user": "nytri.3401",
"operation": "deposit",
"item_id": 20660,
"count": 1,
"coins": 0
},
{
"id": 2708,
"time": "2015-08-29T00:44:12.000Z",
"type": "stash",
"user": "nytri.3401",
"operation": "deposit",
"item_id": 20724,
"count": 1,
"coins": 0
},
{
"id": 2705,
"time": "2015-08-29T00:44:10.000Z",
"type": "stash",
"user": "nytri.3401",
"operation": "deposit",
"item_id": 20506,
"count": 1,
"coins": 0
},
{
"id": 2698,
"time": "2015-08-26T13:34:44.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2689,
"time": "2015-08-25T13:37:19.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2684,
"time": "2015-08-22T22:02:59.000Z",
"type": "upgrade",
"upgrade_id": 17,
"action": "complete"
},
{
"id": 2681,
"time": "2015-08-21T13:35:32.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2678,
"time": "2015-08-20T13:37:50.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2665,
"time": "2015-08-19T22:08:23.000Z",
"type": "stash",
"user": "sassy.8241",
"operation": "deposit",
"item_id": 21161,
"count": 1,
"coins": 0
},
{
"id": 2662,
"time": "2015-08-19T22:02:59.000Z",
"type": "upgrade",
"upgrade_id": 17,
"action": "queued",
"user": "sassy.8241"
},
{
"id": 2660,
"time": "2015-08-19T22:02:36.000Z",
"type": "influence",
"activity": "gifted",
"total_participants": 1,
"participants": [
"sassy.8241"
]
},
{
"id": 2656,
"time": "2015-08-19T20:34:16.000Z",
"type": "influence",
"activity": "gifted",
"total_participants": 1,
"participants": [
"nytri.3401"
]
},
{
"id": 2653,
"time": "2015-08-18T13:36:31.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2641,
"time": "2015-08-15T14:51:05.000Z",
"type": "upgrade",
"upgrade_id": 58,
"action": "complete"
},
{
"id": 2636,
"time": "2015-08-13T14:51:04.000Z",
"type": "upgrade",
"upgrade_id": 58,
"action": "queued",
"user": "nytri.3401"
},
{
"id": 2633,
"time": "2015-08-13T13:17:09.000Z",
"type": "upgrade",
"upgrade_id": 16,
"action": "complete"
},
{
"id": 2629,
"time": "2015-08-12T23:10:40.000Z",
"type": "rank_change",
"user": "Devishly Shell.6102",
"changed_by": "sassy.8241",
"old_rank": "Officer",
"new_rank": "Donor"
},
{
"id": 2628,
"time": "2015-08-12T23:10:38.000Z",
"type": "rank_change",
"user": "Amsera.8179",
"changed_by": "sassy.8241",
"old_rank": "Officer",
"new_rank": "Donor"
},
{
"id": 2627,
"time": "2015-08-12T23:10:15.000Z",
"type": "rank_change",
"user": "nytri.3401",
"changed_by": "sassy.8241",
"old_rank": "Donor",
"new_rank": "Officer"
},
{
"id": 2626,
"time": "2015-08-12T22:04:34.000Z",
"type": "upgrade",
"upgrade_id": 16,
"action": "sped_up",
"user": "nytri.3401"
},
{
"id": 2624,
"time": "2015-08-12T22:04:31.000Z",
"type": "upgrade",
"upgrade_id": 16,
"action": "sped_up",
"user": "nytri.3401"
},
{
"id": 2622,
"time": "2015-08-12T22:04:28.000Z",
"type": "upgrade",
"upgrade_id": 16,
"action": "sped_up",
"user": "nytri.3401"
},
{
"id": 2620,
"time": "2015-08-12T22:04:18.000Z",
"type": "upgrade",
"upgrade_id": 16,
"action": "queued",
"user": "nytri.3401"
},
{
"id": 2618,
"time": "2015-08-12T22:03:59.000Z",
"type": "influence",
"activity": "gifted",
"total_participants": 1,
"participants": [
"nytri.3401"
]
},
{
"id": 2616,
"time": "2015-08-12T17:13:54.000Z",
"type": "joined",
"user": "nytri.3401"
},
{
"id": 2613,
"time": "2015-08-12T13:10:05.000Z",
"type": "invited",
"user": "nytri.3401",
"invited_by": "sassy.8241"
},
{
"id": 2601,
"time": "2015-08-02T13:35:36.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2599,
"time": "2015-08-02T10:30:48.000Z",
"type": "upgrade",
"upgrade_id": 53,
"action": "complete"
},
{
"id": 2593,
"time": "2015-07-31T10:08:13.000Z",
"type": "upgrade",
"upgrade_id": 6,
"action": "complete"
},
{
"id": 2591,
"time": "2015-07-30T18:08:46.000Z",
"type": "upgrade",
"upgrade_id": 53,
"action": "queued",
"user": "Scotty The Engineer.1560"
},
{
"id": 2589,
"time": "2015-07-30T18:08:13.000Z",
"type": "upgrade",
"upgrade_id": 6,
"action": "queued",
"user": "Scotty The Engineer.1560"
},
{
"id": 2444,
"time": "2015-04-12T07:50:51.000Z",
"type": "upgrade",
"upgrade_id": 15,
"action": "complete"
},
{
"id": 2441,
"time": "2015-04-11T13:29:45.000Z",
"type": "upgrade",
"upgrade_id": 15,
"action": "queued",
"user": "Scotty The Engineer.1560"
},
{
"id": 2432,
"time": "2015-04-04T13:35:06.000Z",
"type": "influence",
"activity": "daily_login",
"total_participants": 0,
"participants": []
},
{
"id": 2393,
"time": "2015-03-11T20:54:03.000Z",
"type": "invited",
"user": "magic magician master.8614",
"invited_by": "sassy.8241"
},
{
"id": 2235,
"time": "2014-08-16T16:36:38.000Z",
"type": "rank_change",
"user": "Devishly Shell.6102",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Donor",
"new_rank": "Officer"
},
{
"id": 2233,
"time": "2014-08-16T16:36:33.000Z",
"type": "rank_change",
"user": "Amsera.8179",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Donor",
"new_rank": "Officer"
},
{
"id": 2010,
"time": "2014-03-01T16:56:20.000Z",
"type": "rank_change",
"user": "Scotty The Engineer.1560",
"changed_by": "sassy.8241",
"old_rank": "Donor",
"new_rank": "Co Leader"
},
{
"id": 1645,
"time": "2013-05-27T20:33:09.000Z",
"type": "joined",
"user": "Scotty The Engineer.1560"
},
{
"id": 1641,
"time": "2013-05-27T20:33:02.000Z",
"type": "invited",
"user": "Scotty The Engineer.1560",
"invited_by": "sassy.8241"
},
{
"id": 1631,
"time": "2013-05-21T14:40:05.000Z",
"type": "joined",
"user": "Devishly Shell.6102"
},
{
"id": 1474,
"time": "2013-01-26T22:03:40.000Z",
"type": "joined",
"user": "Amsera.8179"
},
{
"id": 1470,
"time": "2013-01-26T22:03:33.000Z",
"type": "invited",
"user": "Amsera.8179",
"invited_by": "sassy.8241"
},
{
"id": 1438,
"time": "2012-11-28T00:17:49.000Z",
"type": "kick",
"user": "Manus.2795",
"kicked_by": "Manus.2795"
},
{
"id": 1433,
"time": "2012-11-25T18:37:30.000Z",
"type": "invite_declined",
"user": "AdowG.8905",
"declined_by": "AdowG.8905"
},
{
"id": 1428,
"time": "2012-11-22T20:08:33.000Z",
"type": "kick",
"user": "Sheetaan.4135",
"kicked_by": "Sheetaan.4135"
},
{
"id": 1420,
"time": "2012-11-21T15:08:19.000Z",
"type": "kick",
"user": "Chrescerya.4659",
"kicked_by": "Chrescerya.4659"
},
{
"id": 1409,
"time": "2012-11-20T15:41:42.000Z",
"type": "kick",
"user": "Scotty The Engineer.1560",
"kicked_by": "Scotty The Engineer.1560"
},
{
"id": 1407,
"time": "2012-11-20T15:41:06.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Co Leader",
"new_rank": "Leader"
},
{
"id": 1405,
"time": "2012-11-20T15:41:02.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Officer",
"new_rank": "Co Leader"
},
{
"id": 1369,
"time": "2012-11-20T01:49:25.000Z",
"type": "rank_change",
"user": "Manus.2795",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Donor",
"new_rank": "Officer"
},
{
"id": 1367,
"time": "2012-11-20T01:48:33.000Z",
"type": "joined",
"user": "Manus.2795"
},
{
"id": 1364,
"time": "2012-11-20T01:41:01.000Z",
"type": "invited",
"user": "Manus.2795",
"invited_by": "Scotty The Engineer.1560"
},
{
"id": 1230,
"time": "2012-11-18T17:20:55.000Z",
"type": "rank_change",
"user": "Sheetaan.4135",
"changed_by": "Chrescerya.4659",
"old_rank": "Donor",
"new_rank": "Officer"
},
{
"id": 1228,
"time": "2012-11-18T17:16:10.000Z",
"type": "joined",
"user": "Sheetaan.4135"
},
{
"id": 1179,
"time": "2012-11-17T22:45:46.000Z",
"type": "invite_declined",
"user": "Finnguala.6921",
"declined_by": "Scotty The Engineer.1560"
},
{
"id": 1058,
"time": "2012-11-16T20:02:55.000Z",
"type": "invited",
"user": "Finnguala.6921",
"invited_by": "Scotty The Engineer.1560"
},
{
"id": 1054,
"time": "2012-11-16T19:54:15.000Z",
"type": "rank_change",
"user": "Chrescerya.4659",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Donor",
"new_rank": "Officer"
},
{
"id": 1052,
"time": "2012-11-16T19:53:49.000Z",
"type": "joined",
"user": "Chrescerya.4659"
},
{
"id": 1048,
"time": "2012-11-16T19:44:17.000Z",
"type": "invite_declined",
"user": "Finnguala.6921",
"declined_by": "Scotty The Engineer.1560"
},
{
"id": 1045,
"time": "2012-11-16T19:44:05.000Z",
"type": "invited",
"user": "Finnguala.6921",
"invited_by": "Scotty The Engineer.1560"
},
{
"id": 1042,
"time": "2012-11-16T19:43:54.000Z",
"type": "invited",
"user": "Devishly Shell.6102",
"invited_by": "Scotty The Engineer.1560"
},
{
"id": 1039,
"time": "2012-11-16T19:43:36.000Z",
"type": "invited",
"user": "AdowG.8905",
"invited_by": "Scotty The Engineer.1560"
},
{
"id": 1037,
"time": "2012-11-16T19:42:53.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Co Leader",
"new_rank": "Officer"
},
{
"id": 1006,
"time": "2012-11-16T19:34:17.000Z",
"type": "motd",
"user": "Scotty The Engineer.1560",
"motd": "Welcome To Our Friendly Guild, If you need any help in any area please ask in chat and i am sure someone will help you.\n\nalso if you want to do dungeon runs ect please ask an officer and we can arrange a full guild run."
},
{
"id": 1004,
"time": "2012-11-16T19:32:41.000Z",
"type": "motd",
"user": "sassy.8241",
"motd": "Welcome old and new friends have fun thats what its all about"
},
{
"id": 1001,
"time": "2012-11-16T19:31:58.000Z",
"type": "invited",
"user": "Chrescerya.4659",
"invited_by": "Scotty The Engineer.1560"
},
{
"id": 999,
"time": "2012-11-16T19:31:29.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "sassy.8241",
"old_rank": "Leader",
"new_rank": "Officer"
},
{
"id": 997,
"time": "2012-11-16T19:31:23.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Officer",
"new_rank": "Leader"
},
{
"id": 995,
"time": "2012-11-16T19:31:17.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Member",
"new_rank": "Officer"
},
{
"id": 993,
"time": "2012-11-16T19:31:15.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Officer",
"new_rank": "Member"
},
{
"id": 991,
"time": "2012-11-16T19:31:12.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "Scotty The Engineer.1560",
"old_rank": "Member",
"new_rank": "Officer"
},
{
"id": 989,
"time": "2012-11-16T19:30:52.000Z",
"type": "rank_change",
"user": "sassy.8241",
"changed_by": "sassy.8241",
"old_rank": "Leader",
"new_rank": "Member"
},
{
"id": 987,
"time": "2012-11-16T19:30:01.000Z",
"type": "rank_change",
"user": "Scotty The Engineer.1560",
"changed_by": "sassy.8241",
"old_rank": "Member",
"new_rank": "Leader"
},
{
"id": 985,
"time": "2012-11-16T19:29:37.000Z",
"type": "joined",
"user": "Scotty The Engineer.1560"
},
{
"id": 982,
"time": "2012-11-16T19:29:23.000Z",
"type": "invited",
"user": "Sheetaan.4135",
"invited_by": "sassy.8241"
},
{
"id": 979,
"time": "2012-11-16T19:28:46.000Z",
"type": "invited",
"user": "Scotty The Engineer.1560",
"invited_by": "sassy.8241"
},
{
"id": 971,
"time": "2012-11-16T19:28:21.000Z",
"type": "rank_change",
"user": "sassy.8241",
"old_rank": "none",
"new_rank": "leader"
}
], items = [];
$.each(data, function(index, obj) {
items.push('<ul id="' + index + '">')
for (o in obj) {
items.push('<li>' + o + ":"+obj[o] + '</li>');
}
items.push('</ul>');
});
if (items.length < 1) {
items.push('<ul><li>No results , try again!</li></ul>');
}
else $('#res').html(items.join(""));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="res"></div>
&#13;