Java 如何正确映射JSON对象?
这是我的密码:Java 如何正确映射JSON对象?,java,Java,这是我的密码: @JsonIgnoreProperties(ignoreUnknown = true) public class ObjectMapperJSON { public static void main(String[] args) throws IOException, JsonParseException, JsonMappingException { ObjectMapper mapper = new ObjectMapper().configure(Deser
@JsonIgnoreProperties(ignoreUnknown = true)
public class ObjectMapperJSON {
public static void main(String[] args) throws IOException, JsonParseException, JsonMappingException {
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String jsonString = "{\"numberOFiles\":13, \"filename\":\"123.jpg\"}";
/* int temp = Integer.parseInt(jsonString);
*/ ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try{
JsonBean JsonBean = mapper.readValue(jsonString, JsonBean.class);
System.out.println(JsonBean);
jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(JsonBean);
//jsonString = mapper.writeValueAsString(JsonBean);
System.out.println(jsonString);
}
catch (Exception e){
e.printStackTrace();}
}
}
从这个输出是:
文件名[文件名:123.jpg,numberOfFiles:0]
{
文件名:123.jpg,
文件数:0
所以我的问题是,为什么numberOfFiles的值被打印为0?调试将显示直到最后一个syso的值为13。如何正确执行此操作?您的json字符串中有一个输入错误。
将numberOfFiles更新为numberOfFiles。我的天啊。谢谢你。你花了一个小时试图解决这个问题。你值得你的投票。