我想使用Javascript从以下api获取“Subscribercount”的具体值:
然后我想将它存储在2位变量中,我的意思是,如果数字是3256,我想存储:一个变量32个,一个变量56个。
如果你能提供帮助我会很高兴。
答案 0 :(得分:0)
获取api数据,将它们放入变量并运行while循环,使用str.substring()
从字符串中删除两个字符并将其写入变量。
示例:在“splitCount”数组中写入所有拆分号码:
var splitCount = []; // This is the array in which we store our split numbers
//Getting api results via jQuery's GET request
$.get("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCJwchuXd_UWNxW-Z1Cg-liw&key=AIzaSyDUzfsMaYjn7dnGXy9ZEtQB_CuHyii4poc", function(result) {
//result is our api answer and contains the recieved data
//now we put the subscriber count into another variable (count); this is just for clarity
count = result.items[0].statistics.subscriberCount;
//While the subscriber count still has characters
while (count.length) {
splitCount.push(count.substr(0, 2)); //Push first two characters into the splitCount array from line 1
count = count.substr(2); //Remove first two characters from the count string
}
console.log(splitCount) //Output our splitCount array
});
答案 1 :(得分:0)
使用JQuery。
$(document).ready(function(){
$.ajax({
url: "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCJwchuXd_UWNxW-Z1Cg-liw&key=AIzaSyDUzfsMaYjn7dnGXy9ZEtQB_CuHyii4poc",
dataType: "json",
complete: function(data){
var response = data.responseJSON;
var items = response.items;
var item = items[0];
var statistics = item.statistics;
var subscriberCount = statistics.subscriberCount;
console.log(subscriberCount);
}
});
});
如果需要,您可以将所有变量放在一行。