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

当前回答

第一个答案几乎是正确的,但需要的是改变getter方法,不是字段——字段是私有的(不自动检测);此外,如果字段和getter都是可见的,则getter的优先级高于字段。(也有一些方法可以让私有字段可见,但如果你想要getter,那就没什么意义了)

所以getter应该被命名为getWrapper(),或者用以下注解:

@JsonProperty("wrapper")

如果你喜欢getter方法名。

其他回答

这个解决方案在读取json流时是通用的,只需要获取一些字段,而在域类中没有正确映射的字段可以忽略:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)

一个详细的解决方案是使用jsonschema2pojo等工具来自动生成所需的域类,例如从json响应的Schema中生成Student。您可以通过任何在线json到模式转换器来实现后者。

这比所有请参考此属性的工作更好。

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);

要么改变

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

POJO应该定义为

响应类

public class Response {
    private List<Wrapper> wrappers;
    // getter and setter
}

包装器类

public class Wrapper {
    private String id;
    private String name;
    // getters and setters
}

和mapper来读取值

Response response = mapper.readValue(jsonStr , Response.class);

我已经尝试了下面的方法,它适用于这样的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