是否可以在Java中将JSON反序列化为树? [英] Is it possible to deserialize JSON to a tree in Java?
问题描述
我有来自Facebook的JSON响应,我不想将其反序列化为自定义Java对象.主要是因为无法保证其API会保持稳定.一旦他们更改了它,我的反序列化肯定会失败.
I have JSON response from Facebook, which I don't want to deserialize into a custom Java object. Mostly because there is no guarantee that their API will stay stable. Once they change it my deserialization will fail for sure.
我想要的是将其JSON数据反序列化为HashMap<String, Object>
,其中Object
可能是String
或HashMap
.在PHP中,它称为关联数组,它是由 json_decode()
函数生成的.可以在Java中做同样的事情吗?
What I want is to deserialize their JSON data into HashMap<String, Object>
, where Object
may be a String
or a HashMap
. In PHP it's called associative array and it is produced by json_decode()
function. Is it possible to do the same in Java?
推荐答案
当然.看看杰克逊,它可以很容易地做到这一点,例如
Certainly. Have a look at Jackson, it can do this easily enough, e.g.
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> userData = mapper.readValue(jsonData, Map.class);
生成的Map
将嵌套所需深度的多层.
The resulting Map
will nest as many levels deep as is required.
这篇关于是否可以在Java中将JSON反序列化为树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!