在 API 响应中循环字典
Looping through dictionary in API response
我有一本大字典作为API回应,
{totalHits: 379730, currentPage: 1, totalPages: 7595, pageList:
Array(10), foodSearchCriteria: {…}, …}
aggregations: {dataType: {…}, nutrients: {…}}
currentPage: 1
foodSearchCriteria: {pageNumber: 1, numberOfResultsPerPage: 50, pageSize: 50, requireAllWords: false}
foods: (50) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
pageList: (10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
totalHits: 379730
totalPages: 7595
__proto__: Object
我如何循环获取每个页面的食物数组中的每种食物(总计:7595)?
也可以通过 python-requests 完成。
async function fetchFoodData() {
let foods = [];
let morePagesAvailable = true;
let currentPage = 0;
while(morePagesAvailable) {
currentPage++;
const response = await fetch(`http://yourapiurl.io/restlt?page=${currentPage}`)
let food = await response.json();
foods.push(food);
morePagesAvailable = currentPage < total_pages;
}
return foods;
}
我有一本大字典作为API回应,
{totalHits: 379730, currentPage: 1, totalPages: 7595, pageList:
Array(10), foodSearchCriteria: {…}, …}
aggregations: {dataType: {…}, nutrients: {…}}
currentPage: 1
foodSearchCriteria: {pageNumber: 1, numberOfResultsPerPage: 50, pageSize: 50, requireAllWords: false}
foods: (50) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
pageList: (10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
totalHits: 379730
totalPages: 7595
__proto__: Object
我如何循环获取每个页面的食物数组中的每种食物(总计:7595)?
也可以通过 python-requests 完成。
async function fetchFoodData() {
let foods = [];
let morePagesAvailable = true;
let currentPage = 0;
while(morePagesAvailable) {
currentPage++;
const response = await fetch(`http://yourapiurl.io/restlt?page=${currentPage}`)
let food = await response.json();
foods.push(food);
morePagesAvailable = currentPage < total_pages;
}
return foods;
}