我正在尝试解析我从这个网站上下载的 JSON 的“组件”部分: http: //geocoder.opencagedata.com/api.html:
{
"licenses" : [
{
"name" : "CC-BY-SA",
"url" : "http://creativecommons.org/licenses/by-sa/3.0/"
},
{
"name" : "ODbL",
"url" : "http://opendatacommons.org/licenses/odbl/summary/"
}
],
"rate" : {
"limit" : 2500,
"remaining" : 2494,
"reset" : 1434844800
},
"results" : [
{
"annotations" : {
"DMS" : {
"lat" : "22\u00b0 40' 46.34184'' S",
"lng" : "14\u00b0 31' 39.36216'' E"
},
"MGRS" : "33KVQ5147391877",
"Maidenhead" : "JG77gh36hv",
"Mercator" : {
"x" : 1617205.101,
"y" : -2576841.391
},
"OSM" : {
"url" : "http://www.openstreetmap.org/?mlat=-22.67954&mlon=14.52760#map=17/-22.67954/14.52760"
},
"callingcode" : 264,
"geohash" : "k7fqfx6djekju86um1br",
"sun" : {
"rise" : {
"astronomical" : 1434774000,
"civil" : 1434777300,
"nautical" : 1434775620
},
"set" : {
"astronomical" : 1434822420,
"civil" : 1434819120,
"nautical" : 1434820800
}
},
"timezone" : {
"name" : "Africa/Windhoek",
"now_in_dst" : 0,
"offset_sec" : 3600,
"offset_string" : 100,
"short_name" : "WAT"
},
"what3words" : {
"words" : "matriarchs.nano.rotates"
}
},
"components" : {
"city" : "Swakopmund",
"clothes" : "Jet",
"country" : "Namibia",
"country_code" : "na",
"road" : "Nathaniel Maxuilili St (Breite St)",
"state" : "Erongo Region",
"suburb" : "Central"
},
"confidence" : 0,
"formatted" : "Jet, Nathaniel Maxuilili St (Breite St), Swakopmund, Namibia",
"geometry" : {
"lat" : -22.6795394,
"lng" : 14.5276006
}
}
],
"status" : {
"code" : 200,
"message" : "OK"
},
"thanks" : "For using an OpenCage Data API",
"timestamp" : {
"created_http" : "Sat, 20 Jun 2015 21:54:45 GMT",
"created_unix" : 1434837285
},
"total_results" : 1
}
只要该地点实际存在于地图上,我就可以成功地做到这一点。但是,如果该位置不存在,假设我为其提供纬度 1 和经度 1,则此 json 的“组件”部分将不再可用。
如果“组件”没有通过执行以下操作在 json 中返回,我试图防止我的服务器被炸毁:
JsonNode componentsNode = node.path("components");
if (componentsNode.isMissingNode()) {
return "Location Services unavailable";
}
但它所做的只是在进行 isMissingNode 检查之前指向下面的行并说该行有 NullPointerException :
JsonNode componentsNode = node.path("components");
这是有道理的,因为 json 的组件部分不存在。
如果没有杰克逊给我一个空指针异常,我该如何执行此检查?
编辑:
这是我的杰克逊代码:
JsonNode rootNode = m.readTree(responses.getBody());
JsonNode resultNode = rootNode.get("results");
if (resultNode.isArray()) {
JsonNode node = resultNode.get(0);
JsonNode componentsNode = node.path("components");
if (componentsNode.isMissingNode()) {
return "Location Services unavailable";
}
if (!componentsNode.path("city").isMissingNode() && !componentsNode.path("country").isMissingNode()) {
return componentsNode.get("city").textValue() + ", " + componentsNode.get("country").textValue();
}
}
同样根据opencage,如果纬度和经度都等于1,我们有以下json:
{
"documentation" : "http://geocoder.opencagedata.com/api.html",
"licenses" : [
{
"name" : "CC-BY-SA",
"url" : "http://creativecommons.org/licenses/by-sa/3.0/"
},
{
"name" : "ODbL",
"url" : "http://opendatacommons.org/licenses/odbl/summary/"
}
],
"rate" : {
"limit" : 2500,
"remaining" : 2457,
"reset" : 1451174400
},
"results" : [
{
"annotations" : {
"DMS" : {
"lat" : "0\u00b0 0' 42.92604'' N",
"lng" : "0\u00b0 50' 9.45366'' E"
},
"MGRS" : "31MBV5913898681",
"Maidenhead" : "JI09kx07hd",
"Mercator" : {
"x" : 93058.569,
"y" : -1318.477
},
"OSM" : {
"url" : "http://www.openstreetmap.org/?mlat=-0.01192&mlon=0.83596#map=17/-0.01192/0.83596"
},
"geohash" : "kpbxgjbdx30rpj39gksz",
"sun" : {
"rise" : {
"apparent" : 1451109240,
"astronomical" : 1451104680,
"civil" : 1451107860,
"nautical" : 1451106300
},
"set" : {
"apparent" : 1451152860,
"astronomical" : 1451157360,
"civil" : 1451154240,
"nautical" : 1451155800
}
},
"what3words" : {
"words" : "statutes.stout.falsifying"
}
},
"components" : {
"place_of_worship" : "mesjid waskita"
},
"confidence" : 10,
"formatted" : "mesjid waskita",
"geometry" : {
"lat" : -0.0119239,
"lng" : 0.83595935
}
}
],
"status" : {
"code" : 200,
"message" : "OK"
},
"stay_informed" : {
"blog" : "http://blog.opencagedata.com",
"twitter" : "https://twitter.com/opencagedata"
},
"thanks" : "For using an OpenCage Data API",
"timestamp" : {
"created_http" : "Sat, 26 Dec 2015 16:32:06 GMT",
"created_unix" : 1451147526
},
"total_results" : 1
}