Convert JSON to Java Object using Jackson

In the previous post you saw How we converted Java Object to JSON.
Now lets see how to Convert JSON to Java Object using Jackson.

You will need the same dependency or Jar files that were mentioned in the previous post.

In the below example we will convert 2 JSON files.
1. JSON file with only one Employee Object.
2. JSOn file with a list of Employee Objects.

Using the ObjectMapper Class and its method readValue() we will be converting the json to java.

JSON Files

1. Single Employee – (SingleJSON.json)

2. List of Employees – (ListJson.json)

JSONToJavaObject

Output

 

Please Note :
If your Java Class doesnt have a Default Construtor, and you try to use the mapper.readValue to convert JSON to that Java class you will get the following exception

So add a default for any User defined Objects that you need to convert JSON into.

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn