JSON String to Object Mapping

Prabhath kesav picture Prabhath kesav · Oct 30, 2012 · Viewed 18k times · Source

I am having an JSON Response and what I need is to Map the corresponding JSON String to the particular Response class.Is there any tools or framework to do the same.

Response class is:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "0")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {

     @XmlElement(name="0")
     private String firstName;
     @XmlElement(name="1")
     private String lastName;

     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;
     }
}

Json Response String is {"0":{"0":"Rockey","1":"John"}}

I am using Apache CXF Framework with Jettison as the JSON Provider also uses JAXB to wire the data to low bandwidth clients.

Please make a note that I want to convert the number representations to corresponding fields.

Answer

Yashpal Singla picture Yashpal Singla · Oct 30, 2012

You can refer Google-GSON library - https://github.com/google/gson

You can also refer earlier stackoverflow answer - Convert a JSON string to object in Java ME?