如何通过 1 个调用从不同对象获取信息?
How to get information from different objects with 1 call?
我需要包含 3 个字段的标签列表:
tag_name
tag_description
counter_of_posts.
既然counter_of_posts
是tag-object的一个字段,tag_description(摘录)是tag_wiki
的一个字段,如何一次调用获取所需的信息?
这是不可能的 - 您至少需要 2 次调用(如果您有超过 20 个标签,则需要调用更多次)。一次调用 /tags/{tags}/info?site={site}
以获取标签的名称(已给定)和计数器,一次调用 /tags/{tags}/wikis?site={site}
。当然,您可以应用任何您想要的过滤器并更改网站名称和标签名称。这是一个 JavaScript 示例:
(async function() {
const key = '3loXx7CAr2AvrMaHBj6GxQ(('; // not necessary, but it increases daily API quota from 300 to 10000
const sitename = 'Whosebug'; // default, change it to whatever you want
const tags = 'php;javascript;java;jquery;perl;python'; // semicolon-separated, must be =<20
const tagApiUrl = 'https://api.stackexchange.com/2.2/tags/';
const tagInfoFilter = '!-.G.68pp778y';
const tagWikisFilter = '!*Ly1)NvM)n91RtK*';
// First API call: get tag's info
const callTagInfo = await fetch(`${tagApiUrl}${tags}/info?site=${sitename}&filter=${tagInfoFilter}&key=${key}`);
const data_counter = await callTagInfo.json();
// Second API call: get tag's excerpt
const callTagWikis = await fetch(`${tagApiUrl}${tags}/wikis?site=${sitename}&filter=${tagWikisFilter}&key=${key}`);
const data_excerpt = await callTagWikis.json();
for (let i = 0; i < data_counter.items.length; i++) {
const table = document.querySelector('table');
const html = `
<tr>
<td>${data_counter.items[i].name}</td>
<td>${data_excerpt.items.find(name => name.tag_name === data_counter.items[i].name).excerpt}</td>
<td>${data_counter.items[i].count}</td>
</tr>`;
table.insertAdjacentHTML('beforeend', html);
}
console.log('API Quota remaining:', data_excerpt.quota_remaining);
})();
<link rel="stylesheet" href="https://unpkg.com/@Whosebug/stacks/dist/css/stacks.min.css">
<table class="s-table">
<tbody>
<tr>
<th>Tag Name</th>
<th>Excerpt</th>
<th>Number of posts</th>
</tr>
</tbody>
</table>
我需要包含 3 个字段的标签列表:
tag_name
tag_description
counter_of_posts.
既然counter_of_posts
是tag-object的一个字段,tag_description(摘录)是tag_wiki
的一个字段,如何一次调用获取所需的信息?
这是不可能的 - 您至少需要 2 次调用(如果您有超过 20 个标签,则需要调用更多次)。一次调用 /tags/{tags}/info?site={site}
以获取标签的名称(已给定)和计数器,一次调用 /tags/{tags}/wikis?site={site}
。当然,您可以应用任何您想要的过滤器并更改网站名称和标签名称。这是一个 JavaScript 示例:
(async function() {
const key = '3loXx7CAr2AvrMaHBj6GxQ(('; // not necessary, but it increases daily API quota from 300 to 10000
const sitename = 'Whosebug'; // default, change it to whatever you want
const tags = 'php;javascript;java;jquery;perl;python'; // semicolon-separated, must be =<20
const tagApiUrl = 'https://api.stackexchange.com/2.2/tags/';
const tagInfoFilter = '!-.G.68pp778y';
const tagWikisFilter = '!*Ly1)NvM)n91RtK*';
// First API call: get tag's info
const callTagInfo = await fetch(`${tagApiUrl}${tags}/info?site=${sitename}&filter=${tagInfoFilter}&key=${key}`);
const data_counter = await callTagInfo.json();
// Second API call: get tag's excerpt
const callTagWikis = await fetch(`${tagApiUrl}${tags}/wikis?site=${sitename}&filter=${tagWikisFilter}&key=${key}`);
const data_excerpt = await callTagWikis.json();
for (let i = 0; i < data_counter.items.length; i++) {
const table = document.querySelector('table');
const html = `
<tr>
<td>${data_counter.items[i].name}</td>
<td>${data_excerpt.items.find(name => name.tag_name === data_counter.items[i].name).excerpt}</td>
<td>${data_counter.items[i].count}</td>
</tr>`;
table.insertAdjacentHTML('beforeend', html);
}
console.log('API Quota remaining:', data_excerpt.quota_remaining);
})();
<link rel="stylesheet" href="https://unpkg.com/@Whosebug/stacks/dist/css/stacks.min.css">
<table class="s-table">
<tbody>
<tr>
<th>Tag Name</th>
<th>Excerpt</th>
<th>Number of posts</th>
</tr>
</tbody>
</table>