JSON Tutorial

Java object to Json Using Jackson Example

Java object to Json :

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

JavaObjectToJsonFile.java

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.JasonProjectDeomo.bean.Address;
import com.JasonProjectDeomo.bean.Person;

public class JavaObjectToJsonFile {
  public static void main(String[] args) throws JsonGenerationException,
      JsonMappingException, IOException {
    Address address = new Address("Dhananjaypur", "Varanasi", "UP");
    Person person = new Person(1, "Arvind", address);
    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(new File("JacksonFile/dataFour.json"), person);
    System.out.println("--Done--");
  }
}

Output

–Done–