Jackson JSON: get node name from json-tree

前端 未结 5 848
轻奢々
轻奢々 2020-12-02 19:47

How can I receive the node names from a JSON tree using Jackson? The JSON-File looks something like this:

{  
    node         


        
相关标签:
5条回答
  • 2020-12-02 20:19

    Clarification Here:

    While this will work:

     JsonNode rootNode = objectMapper.readTree(file);
     Iterator<Map.Entry<String, JsonNode>> fields = rootNode.fields();
     while (fields.hasNext()) {
        Map.Entry<String, JsonNode> entry = fields.next();
        log.info(entry.getKey() + ":" + entry.getValue())
     }
    

    This will not:

    JsonNode rootNode = objectMapper.readTree(file);
    
    while (rootNode.fields().hasNext()) {
        Map.Entry<String, JsonNode> entry = rootNode.fields().next();
        log.info(entry.getKey() + ":" + entry.getValue())
    }
    

    So be careful to declare the Iterator as a variable and use that.

    Be sure to use the fasterxml library rather than codehaus.

    0 讨论(0)
  • 2020-12-02 20:19
    JsonNode root = mapper.readTree(json);
    root.at("/some-node").fields().forEachRemaining(e -> {
                                  System.out.println(e.getKey()+"---"+ e.getValue());
    
            });
    

    In one line Jackson 2+

    0 讨论(0)
  • 2020-12-02 20:28

    fields() and fieldNames() both were not working for me. And I had to spend quite sometime to find a way to iterate over the keys. There are two ways by which it can be done.

    One is by converting it into a map (takes up more space):

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> result = mapper.convertValue(jsonNode, Map.class);
    for (String key : result.keySet())
    {
        if(key.equals(foo))
        {
            //code here
        }
    }
    

    Another, by using a String iterator:

    Iterator<String> it = jsonNode.getFieldNames();
    while (it.hasNext())
    {
        String key = it.next();
        if (key.equals(foo))
        {
             //code here
        }
    }
    
    0 讨论(0)
  • 2020-12-02 20:34

    For Jackson 2+ (com.fasterxml.jackson), the methods are little bit different:

    Iterator<Entry<String, JsonNode>> nodes = rootNode.get("foo").fields();
    
    while (nodes.hasNext()) {
      Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodes.next();
    
      logger.info("key --> " + entry.getKey() + " value-->" + entry.getValue());
    }
    
    0 讨论(0)
  • 2020-12-02 20:43

    This answer applies to Jackson versions prior to 2+ (originally written for 1.8). See @SupunSameera's answer for a version that works with newer versions of Jackson.


    The JSON terms for "node name" is "key." Since JsonNode#iterator() does not include keys, you need to iterate differently:

    for (Map.Entry<String, JsonNode> elt : rootNode.fields())
    {
        if ("foo".equals(elt.getKey()))
        {
            // bar
        }
    }
    

    If you only need to see the keys, you can simplify things a bit with JsonNode#fieldNames():

    for (String key : rootNode.fieldNames())
    {
        if ("foo".equals(key))
        {
            // bar
        }
    }
    

    And if you just want to find the node with key "foo", you can access it directly. This will yield better performance (constant-time lookup) and cleaner/clearer code than using a loop:

    JsonNode foo = rootNode.get("foo");
    if (foo != null)
    {
        // frob that widget
    }
    
    0 讨论(0)
提交回复
热议问题