Jackson ObjectMapper树:检查isMissingNode()

时间:2015-12-26 15:25:26

标签: java json jackson objectmapper

我试图解析"组件"我从这个网站下面的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的组成部分不存在。

如果Jackson没有给我一个nullpointerexception,我如何执行此检查?

编辑:

这是杰克逊的代码:

        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
}

1 个答案:

答案 0 :(得分:0)

而不是做

JsonNode componentsNode = node.path("components");

尝试做

if (node.at("/results/components").isMissingNode()){...}