How To Parse Json Array from a file
Posted By : Rahul Sharma | 17-Dec-2017
Hi Guys!
Today we talk about the parsing JSON to java object using Jackson-core API. Jackson-core API provides the simplest way to parse json object to java object. By using Jackson-core API we easily parse JSON from a file or object to java object. Jackson-core API provides ObjectMapper class to parse JSON object to java object and also from java object to JSON. Before using this we have to know how works to parse JSON into Java Object.
Jackson-core API maps the JSON fields name to Java fields names to getter and setter methods. it removes the get and set part from java object getter and setter methods and after doing this it converts the first character into lowercase from remaining part of getter and setter methods of java object and called the using getBrand(),setBrand() and getEngineNumber() , setEngineNumber()for parsing it parse the JSON to Java Object. so using Jackson-core API parsing JSON to Java object is very simple.But if we have to parse JSON array object to list , array or any other collections of java object without iteration than Jackson-core API also provide easier but little tricky way to parsing JSON array object to collections of java object.
Here i am showing you how to parse Json array object to collections of java object by two example first is praising from a file and second from JSON Object.
we are using Person.java as POJO for parsing.
public class Person {
private String name;
private String country;
private Long personId;
public String getName() {
return name;
}
public String getCountry() {
return country;
}
public Long getPersonId() {
return personId;
}
public void setName(String name) {
this.name = name;
}
public void setCountry(String country) {
this.country = country;
}
public void setPersonId(Long personId) {
this.personId = personId;
}
}
JacksonParsingFile.java //Example 1 : parsing implementation from a file.
public class JacksonParsing {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String = "Json filepath"
List persons = null;
// Read Default entries from json file.
persons = mapper.readValue(new File(filepath),mapper.getTypeFactory().constructCollectionType(List.class, Person.class));
System.out.println("JSON ------- "+persons);
}
}
JacksonParsingObject.java // Example 2 : parsing implementation from a JSON object.
public class JacksonParsing {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
List persons = null;
// Read Default entries from json file.
persons = mapper.readValue(jsonNodeArray.traverse() /* jsonArray */,
mapper.getTypeFactory().constructCollectionType(List.class, Person.class));
System.out.println("JSON ------- " + persons);
}
}
I Hope By this Example you can understand how internally parsing done by Jackon-core and how can we parse jaon array to collections of java object.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Sharma
Rahul is a bright Web App lead Developer, and has good knowledge of Core Java, Advance Java, Angularjs,javascript. His hobbies are swimming and watching movie.