3

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 + "]";
    }

}
3
  • 3
    Whats the problem and whats the question ? Commented Jan 16, 2014 at 19:20
  • Unable to parse the below mentioned JSON string using jackson. String jsonString = "{\"a3PatientRecordStatusBean\":{\"patientRecordId\":\"1\",\"messageCode\":\"2000\"}}"; When i run the program i get below output: MedicPatientRecordDataStatusBean [a3PatientRecordStatusBean=null] Ideally program should parse the JSON string and load the java objects with the parsed values. PS: I am able to parse outer object but not the inner object/data i.e. {\"patientRecordId\":\"1\",\"messageCode\":\"2000\"} Commented Jan 17, 2014 at 6:01
  • @SvetlinZarev you need to read the complete description to understand the issue. Giving -ve marks is not right. Also, i am not a learner of the field. I have sound experience in the field and somehow silly issue unable to solve. PS. I have used Jackson on other projects. Commented Jan 28, 2014 at 13:56

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need in this case to annotate the field in MedicPatientRecordDataStatusBean 'private A3PatientRecordStatusBean a3PatientRecordStatusBean;' with @JsonUnwrapped

3 Comments

as you suggested, I tried annotating but not luck. Still unable to parse inner data and load java object. @JsonUnwrapped private A3PatientRecordStatusBean a3PatientRecordStatusBean;
try it on the get of the field since it is private
iouardi thanks for reply but @JsonUnwrapped is not solving the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.