我需要将某个JSON字符串转换为Java对象。我正在使用Jackson进行JSON处理。我无法控制输入JSON(我从web服务读取)。这是我的输入JSON:

{"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
}

我的Wrapper类基本上是一个容器对象来获取我的学生列表:

public Class Wrapper {
    private List<Student> students;
    //getters & setters here
}

我一直得到这个错误和“包装器”返回null。我不知道少了什么。有人能帮帮我吗?

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: 
    Unrecognized field "wrapper" (Class Wrapper), not marked as ignorable
 at [Source: java.io.StringReader@1198891; line: 1, column: 13] 
    (through reference chain: Wrapper["wrapper"])
 at org.codehaus.jackson.map.exc.UnrecognizedPropertyException
    .from(UnrecognizedPropertyException.java:53)

当前回答

新的Firebase Android引入了一些巨大的变化;文档副本下面:

[https://firebase.google.com/support/guides/firebase-android]:

更新您的Java模型对象

和2一样。x SDK, Firebase Database会自动将传递给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)

AFTER

public class ChatMessage {
   public String name;
   public String message;
   @Exclude
   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。如果有未知的属性,你可以在类上放一个@ThrowOnExtraProperties注释。

其他回答

在我的情况下,错误是由于以下原因

最初它工作得很好,然后我重命名了一个变量,使 代码的变化,它给了我这个错误。 然后我申请杰克逊无知财产也,但它没有工作。 最后,在重新定义我的getter和setter方法根据 我的变量名称此错误已解决

所以一定要重定义getter和setter。

要么改变

public Class Wrapper {
    private List<Student> students;
    //getters & setters here
}

to

public Class Wrapper {
    private List<Student> wrapper;
    //getters & setters here
}

----或----

将JSON字符串更改为

{"students":[{"id":"13","name":"Fred"}]}

当我们生成getter和setter,特别是以'is'关键字开头的getter和setter时,IDE通常会删除'is'。如。

private boolean isActive;

public void setActive(boolean active) {
   isActive = active;
}

public isActive(){
   return isActive;
}

在我的情况下,我只是改变了getter和setter。

private boolean isActive;

public void setIsActive(boolean active) {
   isActive = active;
}

public getIsActive(){
   return isActive;
}

它能够识别磁场。

我也遇到了类似的问题,唯一的区别是我正在使用YAML文件而不是JSON文件。所以我在创建ObjectMapper时使用YamlFactory()。另外,我的Java POJO的属性是私有的。 然而,所有这些答案中提到的解决方案都没有帮助。我的意思是,它停止抛出错误并开始返回Java对象但Java对象的属性将为null。

我只是做了以下几点:-

objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

而不需要:-

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

现在,我能够反序列化数据从Yaml文件到Java对象。所有属性现在都不是空的。这种解决方案也适用于JSON方法。

问题是你的属性在你的JSON被称为“包装”和你的属性在wrapper .class被称为“学生”。

所以要么…

更正类或JSON中的属性名称。 根据StaxMan的注释注释您的属性变量。 注释setter(如果有的话)