我无法将给定的 Json 字符串转换为 java 对象验证Json格式,正确。@JsonIgnoreProperties(ignoreUnknown = true)public class DevPol { private String id; private Header header; //Setters and Getters}@JsonIgnoreProperties(ignoreUnknown = true)public class Header { private String name; private String lastUpdate; private int priority; private boolean active; //Setters and Getters}import com.fasterxml.jackson.databind.ObjectMapper;public class ConvertJsonToJava { static String apiResult = "[ {\"Id\":\"5899503ad06f7f0008817430\", \"Header\":{ \"name\":\"ClCol\"," + " \"lastupdate\":\"2017-02-07T04:42:34.654Z\", \"priority\":1, \"active\":true } }," + " { \"Id\":\"5899503ad06f7f0008817431\",\"Header\":{ \"name\":\"SysPol\"," + " \"lastupdate\":\"2017-02-07T04:42:34.659Z\", \"priority\":2, \"active\":true } }]"; public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); DevPol[] devPOlArr = mapper.readValue(apiResult, DevPol[].class); for(DevPol devPol: devPOlArr) { System.out.println(devPol.getId()); } }}我期望输出为 Id 值,但是结果为 null null
1 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
问题是 json 字段名称和 java 类字段中的大写字母。
如果可能的话,重命名 json 和 java 中的 'Id' -> 'id'。否则,您应该将 json 属性名称添加到 java 字段:
public class DevPol {
@JsonProperty("Id")
private String Id;
@JsonProperty("Header")
private Header Header;
//Setters and Getters
}
添加回答
举报
0/150
提交
取消