我需要将某个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)

当前回答

Jackson正在抱怨,因为它无法在类Wrapper中找到一个名为“Wrapper”的字段。它这样做是因为JSON对象有一个名为“包装器”的属性。

我认为解决办法是将Wrapper类的字段重命名为“Wrapper”而不是“students”。

其他回答

下面是我试图抑制未知的道具,不想解析。 共享Kotlin代码

val myResponse = jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .readValue(serverResponse, FooResponse::class.java)

这可能是一个非常晚的响应,但只是将POJO更改为这个应该解决问题中提供的json字符串(因为,输入字符串不像你说的那样在你的控制范围内):

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

新的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注释。

如果你想将@JsonIgnoreProperties应用到应用程序中的所有类,那么最好的方法是重写Spring引导默认jackson对象。

在您的应用程序配置文件中定义一个bean来创建这样的杰克逊对象映射器。

@Bean
    public ObjectMapper getObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper;
    }

现在,您不需要标记每个类,它将忽略所有未知属性。

根据这个文档,你可以使用Jackson2ObjectMapperBuilder来构建你的ObjectMapper:

@Autowired
Jackson2ObjectMapperBuilder objectBuilder;

ObjectMapper mapper = objectBuilder.build();
String json = "{\"id\": 1001}";

默认情况下,Jackson2ObjectMapperBuilder禁用错误unrecognizedpropertyexception。