例如,如果我将以下 JSON 反序列化为 JsonNode:
{
"property1": 1,
"property2": 2,
"property3": 3
}
然后用 JsonNode#fields 遍历元素,是否可以保证迭代器将按照属性定义的顺序返回属性(即 property1、property2、property3)?
直觉上我认为答案是否定的,因为 JSON 规范将对象定义为“一组无序的名称/值对”。然而,JSON RFC (RFC 7159)说的是:
JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software.
我还没有找到任何关于 jackson 如何处理这个问题的信息。
最佳答案
查看source code of Jackson's com.fasterxml.jackson.databind.node.ObjectNode
(版本 2.5.0,您可以在网站上搜索不同的版本),保存对象子对象的映射的类型为 LinkedHashMap
文档中说
This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
关于java - Jackson 反序列化为 JsonNode 是否保证属性排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43018483/