JSON Tutorial

Json to Java Object Using Jackson Example

Json to Java :

Here is a simple example program for how to converting  Json file into java object the using jackson.

package com.JasonProjectDeomo.Jackson.JsonRead;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.JasonProjectDeomo.bean.Person;

public class JsonFileToJavaObject {
  public static void main(String[] args) throws JsonParseException,
      JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    // Read from file
    mapper.configure(
        DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Person person = mapper.readValue(new FileInputStream(
        "JacksonFile/dataFour.json"), Person.class);
    System.out.println(person);
  }
}

Output:

{"id":1,"name":"Arvind","address":{"village":"Dhananjaypur","district":"Varanasi","state":"UP"}}

--Done--