我需要一些幫助來捕獲變數中的物件名稱,然后在另一個 API 呼叫中使用該變數。(我不確定這是否可能?)
我正在嘗試從一個 API 中捕獲國家/地區貨幣的名稱,然后在另一個 API 呼叫中使用此變數來獲取該貨幣的匯率。
保存貨幣名稱的初始 API 呼叫的結果如下:
{
"name": {
"common": "United Kingdom",
"official": "United Kingdom of Great Britain and Northern Ireland",
"nativeName": {
"eng": {
"official": "United Kingdom of Great Britain and Northern Ireland",
"common": "United Kingdom"
}
}
},
"tld": [".uk"],
"cca2": "GB",
"ccn3": "826",
"cca3": "GBR",
"cioc": "GBR",
"independent": true,
"status": "officially-assigned",
"unMember": true,
"currencies": {
"GBP": {
"name": "British pound",
"symbol": "£"
}
},
"idd": {
"root": " 4",
"suffixes": ["4"]
},
"capital": ["London"],
"altSpellings": ["GB", "UK", "Great Britain"],
"region": "Europe",
"subregion": "Northern Europe",
"languages": {
"eng": "English"
},
"translations": {
"ara": {
"official": "??????? ??????? ????????? ?????? ???????? ????????",
"common": "??????? ???????"
},
"ces": {
"official": "Spojené království Velké Británie a Severního Irska",
"common": "Spojené království"
},
"cym": {
"official": "United Kingdom of Great Britain and Northern Ireland",
"common": "United Kingdom"
},
"deu": {
"official": "Vereinigtes K?nigreich Gro?britannien und Nordirland",
"common": "Vereinigtes K?nigreich"
},
"est": {
"official": "Suurbritannia ja P?hja-Iiri ühendkuningriik",
"common": "Suurbritannia"
},
"fin": {
"official": "Ison-Britannian ja Pohjois-Irlannin yhdistynyt kuningaskunta",
"common": "Yhdistynyt kuningaskunta"
},
"fra": {
"official": "Royaume-Uni de Grande-Bretagne et d'Irlande du Nord",
"common": "Royaume-Uni"
},
"hrv": {
"official": "Ujedinjeno Kraljevstvo Velike Britanije i Sjeverne Irske",
"common": "Ujedinjeno Kraljevstvo"
},
"hun": {
"official": "Nagy-Britannia és észak-írország Egyesült Királysága",
"common": "Egyesült Királyság"
},
"ita": {
"official": "Regno Unito di Gran Bretagna e Irlanda del Nord",
"common": "Regno Unito"
},
"jpn": {
"official": "グレート·ブリテンおよび北アイルランド連合王國",
"common": "イギリス"
},
"kor": {
"official": "??????? ????? ?? ??",
"common": "??"
},
"nld": {
"official": "Verenigd Koninkrijk van Groot-Brittanni? en Noord-Ierland",
"common": "Verenigd Koninkrijk"
},
"per": {
"official": "??????? ???? ????????? ???? ? ?????? ?????",
"common": "??????"
},
"pol": {
"official": "Zjednoczone Królestwo Wielkiej Brytanii i Irlandii Pó?nocnej",
"common": "Zjednoczone Kr?lestwo"
},
"por": {
"official": "Reino Unido da Gr?-Bretanha e Irlanda do Norte",
"common": "Reino Unido"
},
"rus": {
"official": "Соединенное Королевство Великобритании и Северной Ирландии",
"common": "Великобритания"
},
"slk": {
"official": "Spojené krá?ovstvo Ve?kej Británie a Severnéhoìrska",
"common": "Ve?ká Británia (Spojené krá?ovstvo)"
},
"spa": {
"official": "Reino Unido de Gran Breta?a e Irlanda del Norte",
"common": "Reino Unido"
},
"swe": {
"official": "F?renade konungariket Storbritannien och Nordirland",
"common": "Storbritannien"
},
"urd": {
"official": "?????? ????? ??????? ???? ? ????? ???????",
"common": "?????? ?????"
},
"zho": {
"official": "大不列顛及北愛爾蘭聯合王國",
"common": "英國"
}
},
"latlng": [54, -2],
"landlocked": false,
"borders": ["IRL"],
"area": 242900,
"demonyms": {
"eng": {
"f": "British",
"m": "British"
},
"fra": {
"f": "Britannique",
"m": "Britannique"
}
},
"flag": "????",
"maps": {
"googleMaps": "https://goo.gl/maps/FoDtc3UKMkFsXAjHA",
"openStreetMaps": "https://www.openstreetmap.org/relation/62149"
},
"population": 67215293,
"gini": {
"2017": 35.1
},
"car": {
"signs": ["GB"],
"side": "left"
},
"timezones": ["UTC-08:00", "UTC-05:00", "UTC-04:00", "UTC-03:00", "UTC-02:00", "UTC", "UTC 01:00", "UTC 02:00", "UTC 06:00"],
"continents": ["Europe"],
"flags": {
"png": "https://flagcdn.com/w320/gb.png",
"svg": "https://flagcdn.com/gb.svg"
},
"coatOfArms": {
"png": "https://mainfacts.com/media/images/coats_of_arms/gb.png",
"svg": "https://mainfacts.com/media/images/coats_of_arms/gb.svg"
},
"startOfWeek": "monday",
"capitalInfo": {
"latlng": [51.5, -0.08]
},
"postalCode": {
"format": "@# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA",
"regex": "^(([A-Z]\\d{2}[A-Z]{2})|([A-Z]\\d{3}[A-Z]{2})|([A-Z]{2}\\d{2}[A-Z]{2})|([A-Z]{2}\\d{3}[A-Z]{2})|([A-Z]\\d[A-Z]\\d[A-Z]{2})|([A-Z]{2}\\d[A-Z]\\d[A-Z]{2})|(GIR0AA))$"
}
}
然后我想將貨幣的名稱(在本例中為 GBP)捕獲到一個變數中,這樣我就可以使用它來呼叫另一個 API 以獲取當前匯率。
uj5u.com熱心網友回復:
不確定我是否理解正確,但是像這樣?
const data = {"name":{"common":"United Kingdom","official":"United Kingdom of Great Britain and Northern Ireland","nativeName":{"eng":{"official":"United Kingdom of Great Britain and Northern Ireland","common":"United Kingdom"}}},"tld":[".uk"],"cca2":"GB","ccn3":"826","cca3":"GBR","cioc":"GBR","independent":true,"status":"officially-assigned","unMember":true,"currencies":{"GBP":{"name":"British pound","symbol":"£"}},"idd":{"root":" 4","suffixes":["4"]},"capital":["London"],"altSpellings":["GB","UK","Great Britain"],"region":"Europe","subregion":"Northern Europe","languages":{"eng":"English"},"translations":{"ara":{"official":"??????? ??????? ????????? ?????? ???????? ????????","common":"??????? ???????"},"ces":{"official":"Spojené království Velké Británie a Severního Irska","common":"Spojené království"},"cym":{"official":"United Kingdom of Great Britain and Northern Ireland","common":"United Kingdom"},"deu":{"official":"Vereinigtes K?nigreich Gro?britannien und Nordirland","common":"Vereinigtes K?nigreich"},"est":{"official":"Suurbritannia ja P?hja-Iiri ühendkuningriik","common":"Suurbritannia"},"fin":{"official":"Ison-Britannian ja Pohjois-Irlannin yhdistynyt kuningaskunta","common":"Yhdistynyt kuningaskunta"},"fra":{"official":"Royaume-Uni de Grande-Bretagne et d'Irlande du Nord","common":"Royaume-Uni"},"hrv":{"official":"Ujedinjeno Kraljevstvo Velike Britanije i Sjeverne Irske","common":"Ujedinjeno Kraljevstvo"},"hun":{"official":"Nagy-Britannia és észak-írország Egyesült Királysága","common":"Egyesült Királyság"},"ita":{"official":"Regno Unito di Gran Bretagna e Irlanda del Nord","common":"Regno Unito"},"jpn":{"official":"グレート·ブリテンおよび北アイルランド連合王國","common":"イギリス"},"kor":{"official":"??????? ????? ?? ??","common":"??"},"nld":{"official":"Verenigd Koninkrijk van Groot-Brittanni? en Noord-Ierland","common":"Verenigd Koninkrijk"},"per":{"official":"??????? ???? ????????? ???? ? ?????? ?????","common":"??????"},"pol":{"official":"Zjednoczone Królestwo Wielkiej Brytanii i Irlandii Pó?nocnej","common":"Zjednoczone Kr?lestwo"},"por":{"official":"Reino Unido da Gr?-Bretanha e Irlanda do Norte","common":"Reino Unido"},"rus":{"official":"Соединенное Королевство Великобритании и Северной Ирландии","common":"Великобритания"},"slk":{"official":"Spojené krá?ovstvo Ve?kej Británie a Severnéhoìrska","common":"Ve?ká Británia (Spojené krá?ovstvo)"},"spa":{"official":"Reino Unido de Gran Breta?a e Irlanda del Norte","common":"Reino Unido"},"swe":{"official":"F?renade konungariket Storbritannien och Nordirland","common":"Storbritannien"},"urd":{"official":"?????? ????? ??????? ???? ? ????? ???????","common":"?????? ?????"},"zho":{"official":"大不列顛及北愛爾蘭聯合王國","common":"英國"}},"latlng":[54,-2],"landlocked":false,"borders":["IRL"],"area":242900,"demonyms":{"eng":{"f":"British","m":"British"},"fra":{"f":"Britannique","m":"Britannique"}},"flag":"????","maps":{"googleMaps":"https://goo.gl/maps/FoDtc3UKMkFsXAjHA","openStreetMaps":"https://www.openstreetmap.org/relation/62149"},"population":67215293,"gini":{"2017":35.1},"car":{"signs":["GB"],"side":"left"},"timezones":["UTC-08:00","UTC-05:00","UTC-04:00","UTC-03:00","UTC-02:00","UTC","UTC 01:00","UTC 02:00","UTC 06:00"],"continents":["Europe"],"flags":{"png":"https://flagcdn.com/w320/gb.png","svg":"https://flagcdn.com/gb.svg"},"coatOfArms":{"png":"https://mainfacts.com/media/images/coats_of_arms/gb.png","svg":"https://mainfacts.com/media/images/coats_of_arms/gb.svg"},"startOfWeek":"monday","capitalInfo":{"latlng":[51.5,-0.08]},"postalCode":{"format":"@# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA","regex":"^(([A-Z]\\d{2}[A-Z]{2})|([A-Z]\\d{3}[A-Z]{2})|([A-Z]{2}\\d{2}[A-Z]{2})|([A-Z]{2}\\d{3}[A-Z]{2})|([A-Z]\\d[A-Z]\\d[A-Z]{2})|([A-Z]{2}\\d[A-Z]\\d[A-Z]{2})|(GIR0AA))$"}}
let currency = Object.keys(data.currencies)[0]
console.log(currency)
uj5u.com熱心網友回復:
var json = JSON.parse(data);
var currencyNameGBP = json.currencies.GBP.name;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/429773.html
標籤:javascript 目的
上一篇:如何在VBA中將物件宣告為引數
