Covid API,如何只获取 id
Covid API, how to get only id
我正在制作一个解析加拿大 covid API 的 discord 机器人,但我只想要 ID(参见下面的 JSON 示例:)
{
id: 797158,
province: 'QC',
city: 'Pending',
age: 'Pending',
travel_history: 'Pending',
confirmed_presumptive: 'CONFIRMED',
source: 'https://www.quebec.ca/sante/problemes-de-sante/a-z/coronavirus-2019/situation-coronavirus-quebec/',
date: '2021-02-03 09:05:13',
hr_uid: null
}
我现在想知道如何只获取 JSON 响应的“id”部分,这是我的代码:
const https = require('https');
https.get('https://api.covid19tracker.ca/cases?province=qc', (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
如果有人还可以告诉我如何每 5 分钟向 api 发送一次 get 请求,然后比较最后一次 "id"
和上次请求,我希望如此。
您可以使用“JSON.parse(data).id”从响应数据中获取 id。至于 运行 每 5 分钟一次的请求,您可以使用 setInterval 函数来查看更多详细信息:https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
我正在制作一个解析加拿大 covid API 的 discord 机器人,但我只想要 ID(参见下面的 JSON 示例:)
{
id: 797158,
province: 'QC',
city: 'Pending',
age: 'Pending',
travel_history: 'Pending',
confirmed_presumptive: 'CONFIRMED',
source: 'https://www.quebec.ca/sante/problemes-de-sante/a-z/coronavirus-2019/situation-coronavirus-quebec/',
date: '2021-02-03 09:05:13',
hr_uid: null
}
我现在想知道如何只获取 JSON 响应的“id”部分,这是我的代码:
const https = require('https');
https.get('https://api.covid19tracker.ca/cases?province=qc', (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
如果有人还可以告诉我如何每 5 分钟向 api 发送一次 get 请求,然后比较最后一次 "id"
和上次请求,我希望如此。
您可以使用“JSON.parse(data).id”从响应数据中获取 id。至于 运行 每 5 分钟一次的请求,您可以使用 setInterval 函数来查看更多详细信息:https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval