Since JSON format is essentially a key-value pair grouping, JSON can easily be converted to/from Java maps. This post is about JSON to MAP conversion. Let’s get going.
There are several jar in JACKSON library. For this example, we are only using databind.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.websystique.xml</groupId> <artifactId>JsonJacksonMapMappingDemo</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>JsonJacksonMapMappingDemo</name> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency> </dependencies> </project>
Populate a Map, convert into JSON and write that JSON to a file
package com.websystique.json.jackson; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonJacksonMapMappingDemo { public static void main(String args[]) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> carMap = new HashMap<String, Object>(); carMap.put("car", "Audi"); carMap.put("price", "30000"); carMap.put("model", "2010"); List<String> colors = new ArrayList<String>(); colors.add("Grey"); colors.add("White"); colors.add("Black"); carMap.put("colors", colors); /** * Convert Map to JSON and write to a file */ try { mapper.writeValue(new File("result.json"), carMap); } catch (Exception e) { e.printStackTrace(); } } }
Now open result.json, you should see following output
{"car":"Audi","model":"2010","price":"30000","colors":["Grey","White","Black"]}
Read JSON string from a file and convert JSON to Java Map
package com.websystique.json.jackson; import java.io.File; import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonJacksonMapMappingDemo { public static void main(String args[]) { ObjectMapper mapper = new ObjectMapper(); /** * Read JSON from a file into a Map */ try { Map<String, Object> carMap = mapper.readValue(new File( "result.json"), new TypeReference<Map<String, Object>>() { }); System.out.println("Car : " + carMap.get("car")); System.out.println("Price : " + carMap.get("price")); System.out.println("Model : " + carMap.get("model")); System.out.println("Colors : " + carMap.get("colors")); } catch (Exception e) { e.printStackTrace(); } } }
Below is the output:
Car : Audi Price : 30000 Model : 2010 Colors : [Grey, White, Black]
That’s it. In the next post we will learn about Jackson Tree model api.
References
If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help improve further our learning process.
In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…
Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…
Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…
Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…
Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…
This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…
View Comments