Friday, 15 March 2019

A complete example to convert JSON file into Java Objects

JSON to Java Conversion

      Maven Dependency required for this project

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.6</version>
    </dependency>

Conversion from JSON to JAVA object takes place by following below two steps,


  •        By Creating instance of com.fasterxml.jackson.databind.ObjectMapper
  •        Then using objectMapper.readValue() method to convert JSON to Java object


ObjectMapper mapper = new ObjectMapper();
allQuestions =  mapper.readValue(new File("questions.json"), AllQuestions.class);


Complete Example:

pom.xml

<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.gaurav.json</groupId>
<artifactId>jackson-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
 <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.6</version>
    </dependency>
</dependencies>
</project>

Java Pojo Classes:

AllQuestions.java


package com.gaurav.jackson.model;
public class AllQuestions
{
    private Questions[] questions;
    public Questions[] getQuestions ()
    {
        return questions;
    }
    public void setQuestions (Questions[] questions)
    {
        this.questions = questions;
    }
    @Override
    public String toString()
    {
        return "AllQuestions [questions = "+questions+"]";    }
}



Questions.java


package com.gaurav.jackson.model;
public class Questions
{
    private String questionIdId;
    private QuestionDetails questionDetails;
    public String getQuestionIdId ()
    {
        return questionIdId;    }
    public void setQuestionIdId (String questionIdId)
    {
        this.questionIdId = questionIdId;
    }
    public QuestionDetails getQuestionDetails ()
    {
        return questionDetails;    }
    public void setQuestionDetails (QuestionDetails questionDetails)
    {
        this.questionDetails = questionDetails;    }
    @Override
    public String toString()
    {
        return "Questions [questionIdId = "+questionIdId+", questionDetails = "+questionDetails+"]";    }
}



QuestionDetails.java


package com.gaurav.jackson.model;
public class QuestionDetails
{
    private String data;
    private String index;
    private String exam_id;
    public String getData ()
    {
        return data;
    }
    public void setData (String data)
    {
        this.data = data;
    }
    public String getIndex ()
    {
        return index;
    }
    public void setIndex (String index)
    {
        this.index = index;
    }
    public String getExam_id ()
    {
        return exam_id;
    }
    public void setExam_id (String exam_id)
    {
        this.exam_id = exam_id;
    }
    @Override
    public String toString()
    {
        return "QuestionDetails [data = "+data+", index = "+index+", exam_id = "+exam_id+"]";    }
}

  • Main Java class which will help to read the JSON file and convert that into Java object.



JSON2JavaConverter.java
package com.gaurav.jackson.json;
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.gaurav.jackson.model.AllQuestions;
import com.gaurav.jackson.model.QuestionDetails;
import com.gaurav.jackson.model.Questions;
public class JSON2JavaConverter {
public static void main(String[] args) {
  AllQuestions allQuestions = null;
        ObjectMapper mapper = new ObjectMapper();
        try         {
        allQuestions =  mapper.readValue(new File("questions.json"), AllQuestions.class);
        } catch (JsonGenerationException e)
        {
          e.printStackTrace();
        } catch (JsonMappingException e)
        {
          e.printStackTrace();
        } catch (IOException e)
        {
          e.printStackTrace();
        }
            Questions[] quesArray = allQuestions.getQuestions();
        for(Questions question: quesArray) {
        QuestionDetails questionDetails = question.getQuestionDetails();
        System.out.println("questionDetail Exam ID:"+questionDetails.getExam_id());
        System.out.println("questionDetail Index:"+questionDetails.getIndex());
        System.out.println("questionDetail Data:"+questionDetails.getData());
        }
        }
}


OUTPUT:


questionDetail Exam ID:2345190
questionDetail Index:0
questionDetail Data:{"Which of the following was most probably the first metal to be used in India?", "answers": "[A] Iron", "[B] Copper", "[C] Gold"," [D] Silver", " Answer is": "[B] Copper"}
questionDetail Exam ID:2345191
questionDetail Index:1
questionDetail Data:{"Entomology is the science that studies?", "answers": "[A] Behavior of human beings", "[B] Insects", "[C] The origin and history of technical and scientific terms"," [D] The formation of rocks", " Answer is": "[B] Insects"}
questionDetail Exam ID:2345190
questionDetail Index:2
questionDetail Data:{"For galvanizing iron which of the following metals is used?", "answers": "[A] Aluminium", "[B] Copper", "[C] Lead"," [D] Zinc", " Answer is": "[D] Zinc"}

No comments:

Post a Comment