拜托,我想循环处理来自 API 的 json 响应,当我将其记录到控制台时,我一直未定义。
{
"results": {
"AF": {
"alpha3": "AFG",
"currencyId": "AFN",
"currencyName": "Afghan afghani",
"currencySymbol": "؋",
"id": "AF",
"name": "Afghanistan"
},
"AI": {
"alpha3": "AIA",
"currencyId": "XCD",
"currencyName": "East Caribbean dollar",
"currencySymbol": "$",
"id": "AI",
"name": "Anguilla"
},
"AU": {
"alpha3": "AUS",
"currencyId": "AUD",
"currencyName": "Australian dollar",
"currencySymbol": "$",
"id": "AU",
"name": "Australia"
}
}
}
这是我的代码:
async function currency() {
const response = await fetch(`https://free.currencyconverterapi.com/api/v5/currencies`);
const json = await response.json();
//console.log(json.results);
for (key in json.results) {
for(x in key){
console.log(x)
}
}
}
请问我做错了什么
最佳答案
我相信下面的小修改应该足够了......让我知道它是否有效。
async function currency() {
const response = await fetch(`https://free.currencyconverterapi.com/api/v5/currencies`);
const json = await response.json();
//console.log(json.results);
for (key in json.results) {
for(x in json.results[key]){
console.log(json.results[key][x])
}
}
}
关于javascript - 循环遍历 JSON 响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51088497/