Simple Jackson data binding with generalized objects

Sometimes, you may need to deal with highly dynamic JSON content where you may not be able to map data to a specific Java object, as the structure of the data changes dynamically. In such a scenario, you can use the simple binding APIs offered by the Jackson framework. You will use the Java maps, lists, strings, numbers, Booleans, and nulls for representing dynamic JSON data.

The following code snippet converts a JSON string into a map object. The com.fasterxml.jackson.core.type.TypeReference class is used for passing a generic type definition, which defines a type to bind to, as follows:

String jsonString =  
"{" firstName":"John","lastName":"Chen"}";
ObjectMapper objectMapper = new ObjectMapper();
//properties will store name and value pairs read from jsonString
Map<String, String> properties = objectMapper.readValue(
jsonString, new TypeReference<Map<String, String>>() { });
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.70