如何使用/存储JSON响应的值



我想根据他们的地理IP来确定我网站的访问者来自哪个国家,这样我就可以确定该访问者是否正在访问他们特定国家的网站,并用一个确认弹出窗口提示他们,询问他们是否愿意在不访问的情况下重定向。然而,我的JavaScript知识有限/很少。我将如何存储/使用和返回要从JSON响应中动态使用的特定值?

例如,我想使用以下国家的iso代码"US"。

"country": {
"iso_code": "US",

使用MaxMind的GeoIP2 JavaScript客户端API和以下JavaScript:

<script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js"></script>
<script type="text/javascript">
var onSuccess = function(location){
console.log(
"Lookup successful:nn"
+ JSON.stringify(location, undefined, 4)
);
};
var onError = function(error){
console.log(
"Error:nn"
+ JSON.stringify(error, undefined, 4)
);
};
geoip2.city(onSuccess, onError);
</script>

返回的JSON如下所示:

{
"city": {
"geoname_id": 5368361,
"names": {
"es": "Los Ángeles",
"fr": "Los Angeles",
"ja": "ロサンゼルス",
"pt-BR": "Los Angeles",
"ru": "Лос-Анджелес",
"zh-CN": "洛杉矶",
"de": "Los Angeles",
"en": "Los Angeles"
}
},
"continent": {
"code": "NA",
"geoname_id": 6255149,
"names": {
"fr": "Amérique du Nord",
"ja": "北アメリカ",
"pt-BR": "América do Norte",
"ru": "Северная Америка",
"zh-CN": "北美洲",
"de": "Nordamerika",
"en": "North America",
"es": "Norteamérica"
}
},
"country": {
"iso_code": "US",
"geoname_id": 6252001,
"names": {
"ru": "США",
"zh-CN": "美国",
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos"
}
},
"location": {
"accuracy_radius": 50,
"latitude": 34.0669,
"longitude": -118.3109,
"metro_code": 803,
"time_zone": "America/Los_Angeles"
},
"postal": {
"code": "90020"
},
"registered_country": {
"iso_code": "US",
"geoname_id": 6252001,
"names": {
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "США",
"zh-CN": "美国",
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis"
}
},
"subdivisions": [
{
"iso_code": "CA",
"geoname_id": 5332921,
"names": {
"zh-CN": "加利福尼亚州",
"de": "Kalifornien",
"en": "California",
"es": "California",
"fr": "Californie",
"ja": "カリフォルニア州",
"pt-BR": "Califórnia",
"ru": "Калифорния"
}
}
],
"traits": {
"autonomous_system_number": 7018,
"autonomous_system_organization": "AT&T Services, Inc.",
"isp": "AT&T Services",
"organization": "AT&T Internet Services",
"ip_address": "12.125.142.34"
},
"represented_country": {
"names": {}
}
}

GeoIP2 JavaScript教程

提前谢谢。

你已经有了Json,那么你可以做这样的事情:

var onSuccess = function(location){
console.log(location.country.iso_code);
};

在使用Emeeus answer时,如果您想在本地保存响应,以避免向服务器重复请求或向用户询问两次相同的问题,则可以使用window.localStorage对象。即使用户关闭浏览器,此对象也会在用户浏览器中保留数据,并且数据将可用于您的域。若您想在用户浏览您的网站时存储,并在关闭时清除,请使用sessionStorage

您应该始终向localStorage的字段传递一个字符串。你的代码应该是这样的:

var geoData;
var onSuccess = function(location){
window.localStorage.geoData = JSON.stringify(location);
// ... do whatever you want with location ...
};
// ... on error ...
if (! window.localStorage.geoData)
geoip2.city(onSuccess, onError);
else
onSuccess(JSON.parse(window.localStorage.geoData));

请记住,如果JSON无效,JSON.parse可能会引发错误。我建议你用try {...} catch (e) {geoip2.city(onSuccess, onError)}包围代码,就好像parse失败了,catch就会被执行,可以正常地向服务器请求数据。

相关内容

  • 没有找到相关文章

最新更新