Java json转object报错:Unrecognized field xxx , not marked as ignorable

json转object正常操作

ObjectMapper objectMapper = new ObjectMapper();
// targetObject:转换后的对象
Object targetObject = objectMapper.readValue(jsonString, targetObject.class);

// 如果json中存在目标对象Object不存在的字段,则会报错:Unrecognized field xxx , not marked as ignorable

需要加上下面的语句,才可正常转换

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

也可以直接在整个类上面加上注解

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)

public class newClass {

}

你可能感兴趣的:(json)