Convert JSON String To Java Object, Jettison Example

On this Jettison Example explains about converting a JSON string to java object Using Jettison JSON library

Jettison is a collection of Java APIs (like STaX and DOM) which read and write JSON. This allows nearly transparent enablement of JSON based web services in services frameworks like CXF or XML serialization frameworks like XStream

Reference ->

Here I am showing an example about How to convert a JSON string into Java Object using Jettison library.

Required Libraries

You need to download

  1. Jettison

Following jar must be in classpath

  1. jettison-1.3.3.jar

Project structure

Convert JSON String Into Java Object,  Jettison Example

Here I am creating a pojo class, this will be the converted in to JSON string.

package net.javatips;

public class Student {

private String firstName="Rockey";
private String lastName="Desousa";

public String getFirstName() {
return firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getLastName() {
return lastName;

public void setLastName(String lastName) {
this.lastName = lastName;

public String toString() {
return "Student [firstName=" + firstName + ", lastName=" + lastName + "]";

Jettison Example (Convert JSON string to Student Object)

For converting JSON string into Java Student Object using Jettison library, you can use following method

Unmarshaller unmarshaller = jc.createUnmarshaller();
Student student = (Student) unmarshaller.unmarshal(xmlStreamReader);
package net.javatips;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.mapped.Configuration;
import org.codehaus.jettison.mapped.MappedNamespaceConvention;
import org.codehaus.jettison.mapped.MappedXMLStreamReader;

public class Main {

public static void main(String[] args) throws JAXBException, JSONException, XMLStreamException {

JAXBContext jc = JAXBContext.newInstance(Student.class);

      JSONObject obj =
new JSONObject("{\"student\":{\"firstname\":\"Rockey\",\"lastname\":\"Desousa\"}}");
      Configuration config =
new Configuration();
      MappedNamespaceConvention con =
new MappedNamespaceConvention(config);
      XMLStreamReader xmlStreamReader =
new MappedXMLStreamReader(obj, con);

      Unmarshaller unmarshaller = jc.createUnmarshaller
      Student student =
(Student) unmarshaller.unmarshal(xmlStreamReader);

("Student " + student);
Student Student [firstName=Rockey, lastName=Desousa]


Your email address will not be published. Required fields are marked *