JavaScript Object Notation is a syntax for storing and exchanging of data. JSON is very lightweight hence it has become an alternative to XML.
But to use in a Java program we have to parse a JSON file and convert it into an object form. This blog demonstrate couple of ways how we can convert a JSON file to a java object.
Let us assume the below JSON file.
{
"id" : 1,
"name" : "Manas",
"address" : {
"city" : "Pune",
"state" : "Maharastra",
"country" : "India"
}
}
Corresponding are the Java Entity
1. Student
public class Student {Long id;String name;Address address;Getter/Setter methods}
2. Address
public class Address {private String city;private String state;private String country;Getter/setter methods}
Below outlined two ways of parsing JSON document to Java object.
1. Using Apache commons
This is more of a traditional way of converting JSON to JAVA object. We have to manually map each attribute of the JSON to the java instance variable.
Characteristic of this approach are
1. If we add or delete or modify any attribute from JSON document we have to make corresponding modification to the JSON parsing method to reflect the changes.
2. It is not necessary to have the JSON attribute and instance variable of Java entity to have the same name as mapping is done explicitly.
3. It is not necessary for the JSON document to be well formed.
2. It is not necessary to have the JSON attribute and instance variable of Java entity to have the same name as mapping is done explicitly.
3. It is not necessary for the JSON document to be well formed.
Steps
1. Add below dependency to the pom.xml
<dependency><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId><version>1.1</version></dependency>
2. Parsing method will look as shown below
2. Using fasterXML Jackson
This has become more of a standard way of parsing JSON file.
Characteristic of this approach
1. We do not have to worry if any field is added or removed from JSON document . API will take care of mapping. Hence this is maintainable and scalable
2. The name of attribute in JSON document and instance variable of the java entity should be same.
3. JSON should be well formed.
2. The name of attribute in JSON document and instance variable of the java entity should be same.
3. JSON should be well formed.
Steps
1. Add below dependency to pom.xml
<!-- Dependency for jackson --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.7.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.7.3</version></dependency>
2. The parsing method will look as shown below
We can configure ObjectMapper to have more control over deserialization with on/off property from DeserializationFeature.
Complete project can be found on my github repository https://github.com/manaspratimdas/blog
Resources
More information on DeserializationFeature can be found on below link
https://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/DeserializationFeature.html
1. http://www.tutorialspoint.com/jackson/jackson_objectmapper.htm
2. http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
3. https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ObjectMapper.html
4. http://crunchify.com/java-how-to-parse-jsonobject-and-jsonarrays/
5. http://crunchify.com/how-to-read-json-object-from-file-in-java/
https://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/DeserializationFeature.html
1. http://www.tutorialspoint.com/jackson/jackson_objectmapper.htm
2. http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
3. https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ObjectMapper.html
4. http://crunchify.com/java-how-to-parse-jsonobject-and-jsonarrays/
5. http://crunchify.com/how-to-read-json-object-from-file-in-java/


No comments:
Post a Comment