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. Mostly because there is no guarantee that their API will stay stable. 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 Hash开发者_Python百科Map
. 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);
The resulting Map
will nest as many levels deep as is required.
精彩评论