Java Jackson JsonNode 的get(i)方法疑惑

        String str = "{\"data\":{\"birth_day\":7,\"birth_month\":6},\"errcode\":0,\"msg\":\"ok\",\"ret\":0}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(str);

        for (int i = 0; i < root.size() ; i++) {
            System.out.println(root.get(i));
        }

        for (Iterator<?> iter = root.fieldNames();iter.hasNext();){
            System.out.println(iter.next());
        }

// null
// null
// null
// null
// data
// errcode
// msg
// ret

为什么前面输出的结果都是Null?

阅读 8.8k
1 个回答

public JsonNode get(int index)

Method for accessing value of the specified element of an array node. For other nodes, null is always returned.

For array nodes, index specifies exact location within array and allows for efficient iteration over child elements (underlying storage is guaranteed to be efficiently indexable, i.e. has random-access to elements). If index is less than 0, or equal-or-greater than node.size(), null is returned; no exception is thrown for any index.

参考

http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/

http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/JsonNode.html#get(int)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题