使用 Jackson 读取单个 JSON 事件
我有一个包含多个 JSON 对象的文件,每个对象都可能包含其他对象和数组。
我想将其反序列化,最好使用 Jackson 库,将其反序列化为维护顶级对象分离的数据结构,例如 Java HashMap
对象的数组或列表,其中每个HashMap
将包含文件中单个顶级 JSON 对象的数据。
据我所知,您无法从 Jackson 获得 HashMap
,因此我必须忍受 HashMap
:
List<HashMap<String,Object>> values = new ObjectMapper().readValue(new BufferedReader(new FileReader(path)), new TypeReference<List<HashMap<String, Object>>>() {});
上面的反序列化按预期工作,但是我得到了所有文件数据,而不是单个 JSON 对象的数据,如我所愿。
有什么想法吗?
谢谢!
I have a file with several JSON objects, each of which may contain other objects and arrays.
I would like to deserialize this, preferably using the Jackson library, into a data structure that maintains top-level object separation, e.g. an array or a list of Java HashMap<String, String>
objects, where each HashMap
will contain the data of a single top-level JSON object from the file.
From what I have seen, you can't get a HashMap<String, String>
from Jackson, so I have to put up with a HashMap<String, Object>
:
List<HashMap<String,Object>> values = new ObjectMapper().readValue(new BufferedReader(new FileReader(path)), new TypeReference<List<HashMap<String, Object>>>() {});
The deserialization above works as expected, however I get all the file data and a not a single JSON object's data, as I would like.
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过相当多的努力,这里有一个似乎可行的简单方法:
上面的 ArrayList 中放置的每个
HashMap
对象都对应于一个单独的对象。 (顶级)JSON 事件。readValue() 方法显然不会返回 Map(String, String) 对象。
After quite a bit of effort, here is a simple approach that seems to work:
Each of the
HashMap<String, Object>
objects placed in the aboveArrayList
corresponds to a single (top-level) JSON event.The
readValue()
methods apparently do not return aMap(String, String)
object.