Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to parse JSON having inner objects into Java object using jackson

I am using Spring to develop project and want to parse JSON data coming in string format to the controller. To show case the issue, i have written below small program.

Did goggling enough but no luck. Hoping to get answer on this site. Issue: Unable to parse the inner object i.e. A3PatientRecordStatusBean. Output of the program: MedicPatientRecordDataStatusBean [a3PatientRecordStatusBean=null]

Main program that tries to perform JSON parsing:

public static void main(String[] args) {
        String jsonString = "{\"a3PatientRecordStatusBean\":{\"patientRecordId\":\"1\",\"messageCode\":\"2000\"}}";
        ObjectMapper mapper = new ObjectMapper();
        try {
            MedicPatientRecordDataStatusBean medicPatientRecordDataStatusBean = mapper.readValue(jsonString, MedicPatientRecordDataStatusBean.class);
            System.out.println(medicPatientRecordDataStatusBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Outer class/Object:

@JsonAutoDetect
public class MedicPatientRecordDataStatusBean implements Serializable {
    private static final long serialVersionUID = -4917476398283528449L;

    private A3PatientRecordStatusBean a3PatientRecordStatusBean;

    /**
     * @return the a3PatientRecordStatusBean
     */
    public A3PatientRecordStatusBean getA3PatientRecordStatusBean() {
        return a3PatientRecordStatusBean;
    }

    /**
     * @param a3PatientRecordStatusBean
     *            the a3PatientRecordStatusBean to set
     */
    public void setA3PatientRecordStatusBean(
            A3PatientRecordStatusBean a3PatientRecordStatusBean) {
        a3PatientRecordStatusBean = a3PatientRecordStatusBean;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "MedicPatientRecordDataStatusBean [a3PatientRecordStatusBean="
                + a3PatientRecordStatusBean + "]";
    }

}

Inner object class:

@JsonAutoDetect
public class A3PatientRecordStatusBean implements Serializable {
    private static final long serialVersionUID = -4585669896170562832L;
    private String patientRecordId = "";
    private String messageCode = "";

    /**
     * @return the patientRecordId
     */
    public String getPatientRecordId() {
        return patientRecordId;
    }

    /**
     * @param patientRecordId
     *            the patientRecordId to set
     */
    public void setPatientRecordId(String patientRecordId) {
        this.patientRecordId = patientRecordId;
    }

    /**
     * @return the messageCode
     */
    public String getMessageCode() {
        return messageCode;
    }

    /**
     * @param messageCode
     *            the messageCode to set
     */
    public void setMessageCode(String messageCode) {
        this.messageCode = messageCode;
    }

    @Override
    public String toString() {
        return "A3PatientRecordStatusBean [patientRecordId=" + patientRecordId
                + ", messageCode=" + messageCode + "]";
    }

}

1 Answers

After spending many hours i am able to parse JSON string and load Java objects using Jackson. Below is the modified and working code. In summary, i did following.

  1. Removed @JsonAutoDetect, Serializable, serialVersionUID etc. from the bean classes.
  2. Created the simple beans having only instance variables and set/get methods.
  3. Developed overridden toString() method.

Below are the classes working without any error. Few of the instance variable in the class might be slight different but should be able to convey the message.

MedicPatientRecordDataStatusBean class

public class MedicPatientRecordDataStatusBean {
private int messageCode;
private A3PatientRecordStatusBean a3PatientRecordStatusBean;

public int getMessageCode() {
    return messageCode;
}
public void setMessageCode(int messageCode) {
    this.messageCode = messageCode;
}
public A3PatientRecordStatusBean getA3PatientRecordStatusBean() {
    return a3PatientRecordStatusBean;
}
public void setA3PatientRecordStatusBean(
        A3PatientRecordStatusBean a3PatientRecordStatusBean) {
    this.a3PatientRecordStatusBean = a3PatientRecordStatusBean;
}
@Override
public String toString() {
    return "MedicPatientRecordDataStatusBean [messageCode=" + messageCode
            + ", a3PatientRecordStatusBean=" + a3PatientRecordStatusBean"]";
}

}

A3PatientRecordStatusBean

public class A3PatientRecordStatusBean {
private int patientRecordId;
private int patientProfile;
private int messageCode;

public int getPatientRecordId() {
    return patientRecordId;
}
public void setPatientRecordId(int patientRecordId) {
    this.patientRecordId = patientRecordId;
}
public int getPatientProfile() {
    return patientProfile;
}
public void setPatientProfile(int patientProfile) {
    this.patientProfile = patientProfile;
}
public int getMessageCode() {
    return messageCode;
}
public void setMessageCode(int messageCode) {
    this.messageCode = messageCode;
}

@Override
public String toString() {
    return "A3PatientRecordStatusBean [patientRecordId=" + patientRecordId
            + ", patientProfile=" + patientProfile + ", messageCode="
            + messageCode + "]";
}

}

Hope this will help other developer and will not spend time on JSON string parsing. Finally thank you stack overflow and all for help and suggestions.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!