无法使用jackson将具有内部对象的JSON解析为Java对象

时间:2014-01-16 19:15:02

标签: java json spring spring-mvc jackson

我使用Spring开发项目,并希望将以字符串格式呈现的JSON数据解析到控制器。为了说明问题,我在下面写了一个小程序。

足够瞪眼但没有运气。希望能在这个网站上得到答案。 问题:无法解析内部对象,即A3PatientRecordStatusBean。 程序输出:MedicPatientRecordDataStatusBean [a3PatientRecordStatusBean = null]

尝试执行JSON解析的主程序:

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

外类/对象:

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

}

内部对象类:

@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 个答案:

答案 0 :(得分:3)

花了很多时间后,我能够解析JSON字符串并使用Jackson加载Java对象。以下是修改后的工作代码。 总之,我做了以下。

  1. 从bean类中删除了@JsonAutoDetect,Serializable,serialVersionUID等。
  2. 创建了只包含实例变量和set / get方法的简单bean。
  3. 开发了重写的toString()方法。
  4. 以下是没有任何错误的类。该类中的实例变量很少有可能略有不同,但应该能够传达信息。

    MedicPatientRecordDataStatusBean类

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

    }

    希望这会有助于其他开发人员,而不会花时间进行JSON字符串解析。 最后,谢谢堆栈溢出,以及所有帮助和建议。

答案 1 :(得分:0)

我认为在这种情况下你需要在MedicPatientRecordDataStatusBean'私有A3PatientRecordStatusBean a3PatientRecordStatusBean中注释该字段;'与@JsonUnwrapped

答案 2 :(得分:0)

必须是:

public void setA3PatientRecordStatusBean(A3PatientRecordStatusBean a3PatientRecordStatusBean) {
    this.a3PatientRecordStatusBean = a3PatientRecordStatusBean; ///originally without this
}

您错过了代码中的this。添加它为我修复它。所以我猜杰克逊检测到“二传手”并尝试使用它,但它已经坏了。