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

当前回答

将Wrapper类更改为

public Class Wrapper {

          @JsonProperty("wrapper")  // add this line
          private List<Student> students;
}

这样做的目的是将students字段识别为json对象的包装键。

此外,我个人更喜欢使用Lombok注解的getter和setter作为

@Getter
@Setter
public Class Wrapper {

          @JsonProperty("wrapper")  // add this line
          private List<Student> students;
}

因为我没有同时用Lombok和@JsonProperty测试上面的代码,所以我建议您也将下面的代码添加到Wrapper类中,以覆盖Lombok的默认getter和setter。

public List<Student> getWrapper(){
     return students;
}

public void setWrapper(List<Student> students){
     this.students = students;
}

还可以使用Jackson来反序列化列表。

其他回答

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

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

@JsonProperty("wrapper")

如果你喜欢getter方法名。

为了防止其他人像我一样使用force-rest-api,下面是我如何使用这个讨论来解决它(Kotlin):

var result = forceApi.getSObject("Account", "idhere")
result.jsonMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,  false)
val account: Account = result.`as`(Account::class.java)

看起来force-rest-api使用的是jackson的旧版本。

我曾经遇到过这种情况,当时我的JSON有效负载包括一个API无法识别的属性。解决方案是重命名/删除违规属性。

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

这可能不是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