简体   繁体   English

是否可以将JSON反序列化为Java中的树?

[英]Is it possible to deserialize JSON to a tree in Java?

I have JSON response from Facebook, which I don't want to deserialize into a custom Java object. 我从Facebook,这是我希望反序列化到一个自定义的Java对象的JSON响应。 Mostly because there is no guarantee that their API will stay stable. 主要是因为无法保证其API能够保持稳定。 Once they change it my deserialization will fail for sure. 一旦他们改变它,我的反序列化肯定会失败。

What I want is to deserialize their JSON data into HashMap<String, Object> , where Object may be a String or a HashMap . 我想要的是将他们的JSON数据反序列化为HashMap<String, Object> ,其中Object可以是StringHashMap In PHP it's called associative array and it is produced by json_decode() function. 在PHP中,它被称为关联数组,它由json_decode()函数生成。 Is it possible to do the same in Java? 是否有可能在Java中做同样的事情?

Certainly. 当然。 Have a look at Jackson , it can do this easily enough, eg 看看杰克逊 ,它可以很容易地做到这一点,例如

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> userData = mapper.readValue(jsonData, Map.class);

The resulting Map will nest as many levels deep as is required. 生成的Map将根据需要嵌套多个级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM