简体   繁体   English

如何从 JsonNode 中提取数组?

[英]How to extract an array from a JsonNode?

I have the following input:我有以下输入:

在此处输入图像描述

I want to extract lat and long.我想提取纬度和经度。 I tried the following implementation, but I received null pointer exception for positionNode.get(i + 1).asDouble()我尝试了以下实现,但我收到positionNode.get(i + 1).asDouble()的 null 指针异常

private List<CoordinateBE> getCoordinate(final JsonNode positionNode) {
        
        final List<CoordinateBE> listOfEntrances = new ArrayList<>();
        for (int i = 0; i < positionNode.size(); i = i + 2) {
            final CoordinateBE coordinateBE = new CoordinateBE();
            coordinateBE.setLatitude(positionNode.get(i).asDouble());
            coordinateBE.setLongitude(positionNode.get(i + 1).asDouble());  <--- Null Pointer Exception !!
            listOfEntrances.add(coordinateBE);
        }
        return listOfEntrances;
    }

How can I fix the above implementation?我怎样才能修复上面的实现?

If you are using com.fasterxml.jackson.databind.JsonNode, you can get the expected field by name, instead of using the position如果您使用的是 com.fasterxml.jackson.databind.JsonNode,您可以通过名称获取预期的字段,而不是使用 position

  • positionNode.get("lat").asDouble() for the lat positionNode.get("lat").asDouble() 对于纬度
  • positionNode.get("lng").asDouble() for the lng positionNode.get("lng").asDouble() 对于 lng

Here an example in Java这是 Java 中的示例

    @Test
    public void jsonNodeTest() throws Exception{
        JsonNode positionNode =  new ObjectMapper().readTree("{\"lat\":35.85, \"lng\":139.85}");
        System.out.println("Read simple object " + positionNode.get("lat").asDouble());
        System.out.println("Read simple object " +positionNode.get("lng").asDouble());

        ArrayNode positionNodeArray = (ArrayNode) new ObjectMapper().readTree("[" +
                "{\"lat\":35.85, \"lng\":139.85} , " +
                "{\"lat\":36.85, \"lng\":140.85}" +
                "]");

        // With Stream API
        positionNodeArray.elements().forEachRemaining(jsonNode -> {
            System.out.println("Read in array " + jsonNode.get("lat").asDouble());
            System.out.println("Read in array " +jsonNode.get("lng").asDouble());
        });
        
        // Without Stream API
        Iterator<JsonNode> iter = positionNodeArray.elements();
        while(iter.hasNext()) {
            JsonNode positionNodeInArray = iter.next();
            System.out.println("Read in array with iterator " + positionNodeInArray.get("lat").asDouble());
            System.out.println("Read in array with iterator " +positionNodeInArray.get("lng").asDouble());
        }
    }

Your input "[{"lat":35.65, "lng":139.61}]" is an array of one element.您的输入"[{"lat":35.65, "lng":139.61}]"是一个元素数组。 The loop you're using goes through every other element, because of i = i + 2由于i = i + 2 ,您正在使用的循环遍历所有其他元素

The code inside your setLatitude gets the element at position 0 in your array, which is {"lat":35.65, "lng":139.61} , and converts it into a Double. setLatitude中的代码获取数组中 position 0 处的元素,即{"lat":35.65, "lng":139.61} ,并将其转换为 Double。

The code inside your setLongitude tries to retrieve the element at position 1, which is null. The method asDouble on a null object causes the NullPointerException. setLongitude 中的代码尝试检索位于 position 1 的元素,即 null。null asDouble上的 asDouble 方法导致 NullPointerException。

Here's how you can fix it:修复方法如下:

    private List<CoordinateBE> getCoordinate(final JsonNode positionNodes) {
        
        final List<CoordinateBE> listOfEntrances = new ArrayList<>();
        for (JsonNode positionNode : positionNodes) {
            final CoordinateBE coordinateBE = new CoordinateBE();
            coordinateBE.setLatitude(positionNode.get("lat").asDouble());
            coordinateBE.setLongitude(positionNode.get("lng").asDouble());
            listOfEntrances.add(coordinateBE);
        }
        return listOfEntrances;
    }

Notice that the for loop iterates through every object in positionNodes, and lat and lng are extracted using their name rather than position.请注意,for 循环遍历 positionNodes 中的每个 object,并且使用它们的名称而不是 position 提取 lat 和 lng。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM