如何从JSON API获取信息

时间:2017-11-12 06:08:02

标签: jquery json

这是JSON API,例如你在底部看到会员下面有一个名为Olehead的会员和他的" channel_id":" 377799006849466380",哪个匹配频道称为RANKED(50K)#2(相同的ID),我如何编写代码来显示连接到该频道的人数并将其输出到HTML?。

问候!,

 {  
"channels":[  
  {  
     "position":6,
     "id":"377806110331240449",
     "name":"RANKED (10K) #1"
  },
  {  
     "position":8,
     "id":"377806225343250433",
     "name":"RANKED (10K) #2"
  },
  {  
     "position":7,
     "id":"377806225896898562",
     "name":"RANKED (10K) #2"
  },
  {  
     "position":3,
     "id":"376042187231592452",
     "name":"RANKED (20K) #1"
  },
  {  
     "position":4,
     "id":"377805411018866688",
     "name":"RANKED (20K) #2"
  },
  {  
     "position":5,
     "id":"377805548671991829",
     "name":"RANKED (20K)-#3"
  },
  {  
     "position":0,
     "id":"376043001283084298",
     "name":"RANKED (50K) #1"
  },
  {  
     "position":1,
     "id":"377799006849466380",
     "name":"RANKED (50K) #2"
  },
  {  
     "position":2,
     "id":"377799111296155659",
     "name":"RANKED (50K) #3"
  },
  {  
     "position":9,
     "id":"376042479990079490",
     "name":"Squad #1"
  },
  {  
     "position":10,
     "id":"376042791433928744",
     "name":"Squad #2"
  },
  {  
     "position":11,
     "id":"377807672935710721",
     "name":"Squad #3"
  },
  {  
     "position":12,
     "id":"377807725934936065",
     "name":"Squad-#4"
  },
  {  
     "position":13,
     "id":"377807768800722954",
     "name":"Squad-#5"
  }
  ],
   "instant_invite":null,
   "id":"376042187231592448",
    "members":[  
     {  
     "username":"OleHead",
     "status":"online",
     "mute":false,
     "suppress":false,
     "deaf":false,
     "channel_id":"377799006849466380",
     "avatar_url":"https://cdn.discordapp.com/embed/avatars/4.png",
     "avatar":null,
     "self_deaf":false,
     "discriminator":"6649",
     "self_mute":true,
     "id":"324718336682491904"
  },
  {  
     "username":"pubg-bot",
     "status":"online",
     "bot":true,
     "avatar_url":"https://cdn.discordapp.com/avatars/304978857885171713/ef753a91fcd9cb0d7f68c2f7c829bb6e.jpg",
     "avatar":"ef753a91fcd9cb0d7f68c2f7c829bb6e",
     "discriminator":"6547",
     "id":"304978857885171713"
  },
  {  
     "username":"PUBGOnline.com",
     "status":"online",
     "bot":true,
     "game":{  
        "name":"PUBGOnline"
     },
     "avatar_url":"https://cdn.discordapp.com/avatars/314523338401906688/09aee24f191b0cddf1ea41a8d8d63d8a.jpg",
     "avatar":"09aee24f191b0cddf1ea41a8d8d63d8a",
     "discriminator":"6968",
     "id":"314523338401906688"
  }
   ],
 "name":"PUBGMATES"
 }

1 个答案:

答案 0 :(得分:0)

给出的数据并不能告诉您有多少人连接到频道。这就是你能做的事情:

var apiData = { /* that monstrosity you just pasted */ };

for (var i = 0; i < apiData.members.length; i++) {
    if (apiData.members[i].username === "OleHead") {
        var channelId = apiData.members[i].channel_id;
        for (var c = 0; c < apiData.channels.length; c++) {
            if (apiData.channels[c].id === channelId) {

                $('#target-div').html(apiData.channels[c].name)

            }
        }
    }
}

这会将<div id="target-div"></div>的内部HTML更改为RANKED (50K) #2

除非您能够在不同的API调用中找到与频道相关联的人数,否则您无法使用刚才提供的JSON获取该数据。

  

P.S。 Bleh,Discord看起来有一些令人讨厌的API规格。