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

当前回答

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

@Autowired
Jackson2ObjectMapperBuilder objectBuilder;

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

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

其他回答

这可能不是OP遇到的相同问题,但如果有人带着和我同样的错误来到这里,那么这将帮助他们解决问题。当我使用来自不同依赖项的ObjectMapper作为JsonProperty注释时,我得到了与OP相同的错误。

如此:

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

这对我来说非常有效

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

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

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

我已经尝试了下面的方法,它适用于这样的JSON格式读取杰克逊。 使用已经建议的解决方案:用@JsonProperty("wrapper")注释getter

你的包装类

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

然而,这将为您提供格式的输出:

{"wrapper":{"student":[{"id":13,"name":Fred}]}}

更多信息请访问https://github.com/FasterXML/jackson-annotations

可以通过两种方式实现:

将POJO标记为忽略未知属性 @JsonIgnoreProperties(ignoreUnknown = true) 配置ObjectMapper序列化/反序列化POJO/json,如下所示: ObjectMapper mapper =new ObjectMapper(); // Jackson版本1。X mapper.configure (DeserializationConfig.Feature。FAIL_ON_UNKNOWN_PROPERTIES、假); // Jackson版本2。X mapper.configure (DeserializationFeature。FAIL_ON_UNKNOWN_PROPERTIES假)