com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段

时间:2014-05-05 10:03:14

标签: java json jackson

我遇到了反序列化问题:

这是我的班级:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

我想要反序列化的JSON是:

{"ResObj":{"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"},"ResInt":0}

我得到了这个例外:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ResObj" , not marked as ignorable (0 known properties: ])
 at [Source: java.io.StringReader@1f758500; line: 1, column: 20] (through reference chain: ["ResObj"])

我不想添加:

@JsonIgnoreProperties(ignoreUnknown = true)

因为我想获得ResObj ...

如果我添加注释,它会通过,但会将其设置为null ..这是我不想要的。

6 个答案:

答案 0 :(得分:39)

如果您不想在bean中使用setter并且只使用字段和getter,则可以使用ObjectMapper的可见性检查器来允许字段可见性。像下面这样的东西

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));

答案 1 :(得分:7)

你需要Setter方法来允许Jackson设置属性,你需要更改json中的字段以小写字母开头:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public void setResObj(Object ResObj) {
        this.ResObj = ResObj;
    }

    // ...
}

{"resObj":{"clientNum":"12345","serverNum":"78945","idNum":"020252"},"resInt":0}

JSON更改的原因是Jackson bean序列化将反映在类上,当它看到getXyz()和setXyz()时,方法会将这些映射到Json字段名称“xyz”(而不是“Xyz”) )。

我认为有几种方法可以覆盖这种行为,一种是使用杰克逊注释之一。

答案 2 :(得分:4)

我认为你应该试试这个

public class Response {
    @JsonProperty
    private Object ResObj;
    @JsonProperty
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

它将使用UnrecognizedPropertyExceptions

解决您的问题

答案 3 :(得分:0)

您需要为ResObj中的信息定义另一个类{" ClientNum":" 12345"," ServerNum":" 78945" ,#&34; IDNUM":" 020252"}。否则杰克逊无法确定如何反序列化。

答案 4 :(得分:0)

public class Response {
    public Object ResObj;
    public int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

使用此方法解决上述问题。

答案 5 :(得分:0)

我已经使用Jackson图书馆解决了这个问题。这是我的代码段。

**Main Class with JSON String in all lower case:**

 public class MainClass {

public static void main(String[] args) throws JsonParseException, 
    JsonMappingException, IOException {

    String jsonStr = "{\r\n" + "    \"resObj\": {\r\n" + "      \"clientNum\": 
               \"12345\",\r\n"
            + "     \"serverNum\": \"78945\",\r\n" + "      \"idNum\": 
          \"020252\"\r\n" + "   },\r\n"
            + " \"resInt\": 0\r\n" + "}";

    ObjectMapper mapper = new ObjectMapper();

    MyPojo details = mapper.readValue(jsonStr, MyPojo.class);

    System.out.println("value of clientNum: " + details.getResObj().getClientNum());
    System.out.println("value of getServerNum: " + 
               details.getResObj().getServerNum());
    System.out.println("value of getIdNum: " + details.getResObj().getIdNum());
    System.out.println("value of getResInt: " + details.getResInt());

} }

**MyPojo Class:**

 public class MyPojo {
private ResObj resObj;

private String resInt;

public ResObj getResObj() {
    return resObj;
}

  public String getResInt() {
       return resInt;   } }

**ResObj class:**


public class ResObj {
private String serverNum;

private String idNum;

private String clientNum;

public String getServerNum() {
    return serverNum;
}

public String getIdNum() {
    return idNum;
}

public String getClientNum() {
    return clientNum;
} }

**RESULT**

  value of clientNum: 12345
  value of getServerNum: 78945
  value of getIdNum: 020252
  value of getResInt: 0

NOTE: I have removed Setters in classes & there is no effect on the result.