【问题标题】:Jackson ObjectMapper Tree: check for isMissingNode()Jackson ObjectMapper 树:检查 isMissingNode()
【发布时间】:2015-12-26 15:25:25
【问题描述】:

我正在尝试解析以下我从该网站下载的 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 给我一个空指针异常,我如何执行此检查?

编辑:

这是我的杰克逊代码:

        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
}

【问题讨论】:

  • 能否提供更多关于path 方法调用的Java 代码?因为从 javadoc 来看,如果节点不存在(fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/…),path 方法将不会返回 NPE,所以这意味着您的 node 变量改为 null。
  • 你好,我添加了更多代码。
  • 你试过调试你的代码吗?看起来resultNode.get(0) 在你的情况下返回null ..检查resultNode 长度..
  • 复制粘贴你的代码和json。删除了 components 属性。我得到"Location Services unavailable"。使用 Java 8 和 Jackson 2.6.0

标签: java json jackson objectmapper


【解决方案1】:

而不是做

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

尝试做

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

【讨论】:

    猜你喜欢
    • 2014-05-06
    • 2016-11-03
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2018-11-09
    • 2016-09-21
    相关资源
    最近更新 更多