【问题标题】:Unable to parse JSON having inner objects into Java object using jackson无法使用杰克逊将具有内部对象的 JSON 解析为 Java 对象
【发布时间】:2014-01-16 19:15:01
【问题描述】:

我正在使用 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 + "]";
    }

}

【问题讨论】:

  • 有什么问题和问题是什么?
  • 无法使用 jackson 解析下面提到的 JSON 字符串。 String jsonString = "{\"a3PatientRecordStatusBean\":{\"patientRecordId\":\"1\",\"messageCode\":\"2000\"}}";当我运行程序时,我得到以下输出: MedicPatientRecordDataStatusBean [a3PatientRecordStatusBean=null] 理想情况下,程序应该解析 JSON 字符串并使用解析值加载 java 对象。 PS:我能够解析外部对象,但不能解析内部对象/数据,即 {\"patientRecordId\":\"1\",\"messageCode\":\"2000\"}
  • @SvetlinZarev 您需要阅读完整的描述才能理解问题。给 -ve 分数是不对的。另外,我不是该领域的学习者。我在该领域有丰富的经验,但不知何故愚蠢的问题无法解决。 PS。我在其他项目中使用过 Jackson。

标签: java json spring spring-mvc jackson


【解决方案1】:

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

  1. 从 bean 类中删除了 @JsonAutoDetect、Serializable、serialVersionUID 等。
  2. 创建了只有实例变量和 set/get 方法的简单 bean。
  3. 开发了重写的 toString() 方法。

以下是没有任何错误的类。类中的一些实例变量可能略有不同,但应该能够传达信息。

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 字符串解析上。 最后感谢堆栈溢出以及所有的帮助和建议。

【讨论】:

    【解决方案2】:

    我认为在这种情况下您需要注释 MedicPatientRecordDataStatusBean 'private A3PatientRecordStatusBean a3PatientRecordStatusBean;' 中的字段用@JsonUnwrapped

    【讨论】:

    • 正如你所建议的,我尝试了注释但没有运气。仍然无法解析内部数据并加载 java 对象。 @JsonUnwrapped 私有 A3PatientRecordStatusBean a3PatientRecordStatusBean;
    • 在获取字段时尝试一下,因为它是私有的
    • iouardi 感谢您的回复,但 @JsonUnwrapped 并没有解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多