Java Jackson with JSON:无法识别的字段,未标记为可忽略
我需要将某个JSON字符串转换为Java对象。我使用Jackson处理JSON。我无法控制输入JSON(我从web服务读取)。这是我的输入JSON:Java Jackson with JSON:无法识别的字段,未标记为可忽略,java,json,data-binding,jackson,Java,Json,Data Binding,Jackson,我需要将某个JSON字符串转换为Java对象。我使用Jackson处理JSON。我无法控制输入JSON(我从web服务读取)。这是我的输入JSON: {"wrapper":[{"id":"13","name":"Fred"}]} 下面是一个简化的用例: private void tryReading() { String jsonStr = "{\"wrapper\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}"; ObjectMapper ma
{"wrapper":[{"id":"13","name":"Fred"}]}
下面是一个简化的用例:
private void tryReading() {
String jsonStr = "{\"wrapper\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";
ObjectMapper mapper = new ObjectMapper();
Wrapper wrapper = null;
try {
wrapper = mapper.readValue(jsonStr , Wrapper.class);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("wrapper = " + wrapper);
}
我的实体类是:
public Class Student {
private String name;
private String id;
//getters & setters for name & id here
}
我的包装器类基本上是一个容器对象,用于获取我的学生列表:
public Class Wrapper {
private List<Student> students;
//getters & setters here
}
Jackson在抱怨,因为它在类包装器中找不到名为“包装器”的字段。之所以这样做是因为JSON对象有一个名为“wrapper”的属性
我认为解决方法是将包装器类的字段重命名为“Wrapper”而不是“students”。第一个答案几乎正确,但需要的是更改getter方法,而不是字段——字段是私有的(不是自动检测的);此外,如果两个字段都可见,则getter优先于字段(也有一些方法可以使私有字段可见,但如果希望使用getter,则没有太多意义) 因此,getter应该命名为
getWrapper()
,或者用以下注释:
@JsonProperty("wrapper")
如果您喜欢按原样命名getter方法。可以使用Jackson的类级注释:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties
class { ... }
它将忽略POJO中未定义的所有属性。当您只是在JSON中查找几个属性而不想编写整个映射时,这非常有用。更多信息请访问。如果要忽略任何未声明的属性,应编写:
@JsonIgnoreProperties(ignoreUnknown = true)
没有人提到过,我想我会 问题是JSON中的属性称为“wrapper”,wrapper.class中的属性称为“students” 所以要么
我已经尝试了下面的方法,它适用于Jackson的JSON格式读取。 使用已经建议的解决方案:使用
@JsonProperty(“包装器”)
你的包装类
public Class Wrapper{
private List<Student> students;
//getters & setters here
}
public Class Wrapper{
private StudentHelper students;
//getters & setters here
// Annotate getter
@JsonProperty("wrapper")
StudentHelper getStudents() {
return students;
}
}
public class StudentHelper {
@JsonProperty("Student")
public List<Student> students;
//CTOR, getters and setters
//NOTE: If students is private annotate getter with the annotation @JsonProperty("Student")
}
public class Wrapper {
private String id;
private String name;
// getters and setters
}
有关更多信息,请参阅
希望这对您有所帮助您可以使用
ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
它将忽略所有未声明的属性。此解决方案在读取json流时是通用的,只需要获取一些字段,而域类中未正确映射的字段可以忽略:
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
详细的解决方案是使用jsonschema2pojo等工具从json响应的模式自动生成所需的域类,如Student。您可以通过任何在线json到模式转换器来实现后者。这对我来说非常有效
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@JsonIgnoreProperties(ignoreUnknown=true)
注释没有。此功能比所有功能都好,请参考此属性
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
projectVO = objectMapper.readValue(yourjsonstring, Test.class);
如果您使用的是Jackson 2.0
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
POJO应定义为
class Wrapper {
List<Student> wrapper;
}
响应类
public class Response {
private List<Wrapper> wrappers;
// getter and setter
}
和映射器读取值
Response response = mapper.readValue(jsonStr , Response.class);
对我来说,最有效的办法是将房产公之于众。它为我解决了问题。要么改变
public Class Wrapper {
private List<Student> students;
//getters & setters here
}
就我而言,唯一的一行
@JsonIgnoreProperties(ignoreUnknown=true)
也不管用
加上
@JsonInclude(Include.NON_EMPTY)
杰克逊2.4.0这对我来说非常有效
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
通过简单地更改POJO类的setter和getter方法的签名,我解决了这个问题。我所要做的就是更改getObject方法,以匹配映射程序所寻找的内容。在我的例子中,最初我有一个getImageUrl,但是JSON数据有image\uURL,这会使映射程序崩溃。我将setter和getter都更改为getImage\uurl和setImage\uurl
public Class Student {
public String name;
public String id;
//getters & setters for name & id here
}
希望这能有所帮助。使用Jackson 2.6.0,这对我很有用:
private static final ObjectMapper objectMapper =
new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
和设置:
@JsonIgnoreProperties(ignoreUnknown = true)
它使用以下代码为我工作:
ObjectMapper mapper =new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
根据,您可以忽略选定字段或所有uknown字段:
// to prevent specified fields from being serialized or deserialized
// (i.e. not include in JSON output; or being set even if they were included)
@JsonIgnoreProperties({ "internalId", "secretKey" })
// To ignore any unknown properties in JSON input without exception:
@JsonIgnoreProperties(ignoreUnknown=true)
你的意见
{"wrapper":[{"id":"13","name":"Fred"}]}
指示它是一个对象,具有名为“wrapper”的字段,该字段是学生的集合。所以我的建议是
Wrapper = mapper.readValue(jsonStr , Wrapper.class);
其中,包装器
定义为
class Wrapper {
List<Student> wrapper;
}
类包装器{
列表包装器;
}
新的Firebase Android引入了一些巨大的变化;在文件副本下方:
[:
更新Java模型对象
与2.x SDK一样,Firebase数据库将自动将传递给DatabaseReference.setValue()
的Java对象转换为JSON,并可以使用DataSnapshot.getValue()
将JSON读入Java对象
在新的SDK中,当使用DataSnapshot.getValue()
将JSON读入Java对象时,JSON中的未知属性现在默认被忽略,因此您不再需要@JsonIgnoreExtraProperties(ignoreUnknown=true)
要在将Java对象写入JSON时排除字段/getter,注释现在称为@exclude
,而不是@JsonIgnore
BEFORE
@JsonIgnoreExtraProperties(ignoreUnknown=true)
public class ChatMessage {
public String name;
public String message;
@JsonIgnore
public String ignoreThisField;
}
dataSnapshot.getValue(ChatMessage.class)
如果JSON中有一个额外属性不在Java类中,您将在日志文件中看到此警告:
W/ClassMapper: No setter/field for ignoreThisProperty found on class com.firebase.migrationguide.ChatMessage
您可以通过在类上添加
@IgnoreExtraProperties
注释来消除此警告。如果您希望Firebase数据库像在2.x SDK中那样运行,并在存在未知属性时引发异常,则可以在类上添加@ThrowOnExtraProperties
注释。将现场学员注释为bel因为json属性和java属性的名称不匹配
public Class Wrapper {
@JsonProperty("wrapper")
private List<Student> students;
//getters & setters here
}
公共类包装器{
@JsonProperty(“包装器”)
私人名单学生;
//这里是接球手和接球手
}
可以通过两种方式实现:
@JsonIgnoreProperties(ignoreUnknown = true)
ObjectMapper mapper =new ObjectMapper();
// for Jackson version 1.X
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// for Jackson version 2.X
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
这可能是一个非常晚的响应,但只要将POJO更改为该值,就可以解决问题中提供的json字符串(因为
public Class Wrapper {
@JsonProperty("wrapper")
private List<Student> students;
//getters & setters here
}
@JsonIgnoreProperties(ignoreUnknown = true)
ObjectMapper mapper =new ObjectMapper();
// for Jackson version 1.X
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// for Jackson version 2.X
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
public class Wrapper {
private List<Student> wrapper;
//getters & setters here
}
public Class Student {
public String name;
public String id;
//getters & setters for name & id here
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper; //org.codehaus.jackson:jackson-mapper-asl:1.8.8
import com.fasterxml.jackson.annotation.JsonProperty; //com.fasterxml.jackson.core:jackson-databind:2.2.3
FAIL_ON_UNKNOWN_PROPERTIES (default: true)
Used to control whether encountering of unknown properties (one for which there is no setter; and there is no fallback "any setter" method defined using @JsonAnySetter annotation) should result in a JsonMappingException (when enabled), or just quietly ignored (when disabled)
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
@Provider
public class CustomObjectMapperProvider implements ContextResolver<ObjectMapper> {
private ObjectMapper objectMapper;
@Override
public ObjectMapper getContext(final Class<?> cls) {
return getObjectMapper();
}
private synchronized ObjectMapper getObjectMapper() {
if(objectMapper == null) {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
return objectMapper;
}
}