我正在使用 ipinfo.io 库检查用户所在的国家/地区,一切正常。
我正在使用这段代码来提取数据
$.get("https://ipinfo.io?token=0000000000", function(response) {
console.log(response.ip, response.country);
}, "jsonp")
我在 https://ipinfo.io/developers/full-country-names 中读到我可以使用完整的国家/地区名称 json 文件,但我不知道如何将其映射在一起,甚至不知道从哪里开始。这里有什么建议吗?
谢谢!
最佳答案
一种方法是将该 names.json 文件本地存储在您的项目环境中。然后您可以检查国家代码是否是该对象文字的键。如果找到,您可以打印该值。
像这样:
var countries = {"BD": "Bangladesh", "BE": "Belgium", "BF": "Burkina Faso"}; // your json object names.json
var foundCountryCode = "BE"; // the country code you found
var countryName = countries[foundCountryCode]; // here you get the name
console.log(countryName); // do stuff
关于javascript - 将国家代码转换为国家全名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55606119/