无法从 JSON 字符串(带有内部数组)中获取 Java 对象

Can not get Java object from JSON string(with inner arrays)

我正在尝试从具有内部数组的 JSON 字符串中获取 Java 对象,有很多相同的问题,但 none 无法解决我的问题。现在在控制台中我得到 MethodPackage.JsonDeserialize@6580cfdd (我正在使用 objectmapper) 我的目标是在 json 中单独获取值以进行一些操作

下面是我的完整代码:

JSONstring:
{
"status": 1,
"message": "ok",
"sheduleCod": "NOST_A_Persons_m_noaccum",
"algorithms": [{
    "cod": "No_st_alg_1",
    "kcp": "U6000427",
    "dtBeg": "2017-11-01 00:00:00",
    "dtEnd": "2017-12-01 00:00:00"
}, {
    "cod": "No_st_alg_2",
    "kcp": "U6000427",
    "dtBeg": "2017-11-01 00:00:00",
    "dtEnd": "2017-12-01 00:00:00"
}, {
    "cod": "No_st_alg_3",
    "kcp": "U6000427",
    "dtBeg": "2017-11-01 00:00:00",
    "dtEnd": "2017-12-01 00:00:00"
}]
}

          Main.class

String jsonString = response.toString();
JsonDeserialize deserialize = objectMapper.readValue(jsonString, JsonDeserialize.class);
System.out.println(deserialize);}

JsonDeserialize.class 
public class JsonDeserialize {
private String status;
private String message;
private String sheduleCod;
private List<Algorithm> algorithms;

            in JsonDeserialize.class 

public class JsonDeserialize {
private String status;
private String message;
private String sheduleCod;
private List<Algorithm> algorithms;
public JsonDeserialize(String status, String message, String sheduleCod, List<Algorithm> algorithms) {
    this.status = status;
    this.message = message;
    this.sheduleCod = sheduleCod;
    this.algorithms = algorithms;
}

..... and then getters and setters

                  Algorithm.class

public class Algorithm {
private String cod;
private String kcp;
private String dtBeg;
private String dtEnd;

public Algorithm(String cod, String kcp, String dtBeg, String dtEnd) {
    this.cod = cod;
    this.kcp = kcp;
    this.dtBeg = dtBeg;
    this.dtEnd = dtEnd;
}
public Algorithm () {

}

输出 MethodPackage.JsonDeserialize@6580cfdd 意味着您打印的是引用而不是对象的值。

要解决此问题,请重写 JsonDeserialize class 中的 toString 方法,如下所示:

@Override
public String toString() {
    String values = ""; // you could also use a StringBuilder here
    values += "Status: " + status + "\n";
    values += "Message: " + message + "\n";
    // ....
    return values;
}

或使用:

System.out.println(deserialize.getStatus())
System.out.println(deserialize.getMessage());
// ...

如果您使用的是 Jackson 或 GS​​ON,您只需创建与数据结构相匹配的 POJO,它就会自动运行。此外,在这些 pojo 中,您可以使属性名称与 JSON 具有的名称完全相同,或者使用 jackson 注释让您为每个属性提供 JSON 属性 名称对象 属性。但主要是我没有看到您展示的 POJOS 的任何 getter 和 setter,而且您很可能确实需要这些。请注意使用正确的 'bean naming' 约定命名属性。