jackson java无法识别的字段未标记为可忽略

时间:2016-06-13 04:13:48

标签: java json jackson

错误代码

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: 
Unrecognized field "id" (Class JacksonTester$Student), not
marked as ignorable
 at [Source: [B@40334c25; line: 2, column: 8] 
(through reference chain: Student["id"])

我有以下JSON文件:

{
  "id": "0",
  "title": "0",
  "externalId": "0",
  "externalLink": "0",
  "sourceApplication": "0",
  "content": "0",
  "summaryContent": "0",
  "publishedDate": "0",
  "harvestDate": "0",
  "languageId": "0",
  "regionId": "0",
  "postStatus": "0"
}

我的代码是

JacksonTester.java:

public class JacksonTester {
public static void main(String args[]) {

    ObjectMapper mapper = new ObjectMapper();

    // map json to student

    try {

        byte[] jsonData = Files.readAllBytes(Paths.get("output_json.txt"));
        Student student = mapper.readValue(jsonData, Student.class);
        System.out.println(student);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

static class Student {
    String id;
    String title;
    String externalId;
    String externalLink;
    String sourceApplication;
    String content;
    String summaryContent;
    String publishedDate;
    String harvestDate;
    String languageId;
    String regionId;
    String postStatus;

    public Student() {
    }

}
}

4 个答案:

答案 0 :(得分:1)

您需要拥有这些字段的setter或者接受这些字段作为参数的构造函数(来自Java 8的+ approriate annotations或-parameters和jackson-module-parameter-names  模块):

public static class Student {
    ...
    String postStatus;   

    public setPostStatus(postStatus) {
        this.postStatus = postStatus;
    }

    ...
}

答案 1 :(得分:0)

杰克逊无法进入学生领域。

为学生实施公共getter和setter并且它可以正常工作。

答案 2 :(得分:0)

我对这个问题进行了排序,并且工作正常。这是我的相同代码。

 **MainClass.java:**

 public class MainClass {

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

    String jsonStr = "{\r\n" + "    \"id\": \"168\",\r\n" + "   \"title\": \"Mr\",\r\n"
            + " \"externalId\": \"247518\",\r\n" + "    \"externalLink\": \"www.gmail.com\",\r\n"
            + " \"sourceApplication\": \"adsense\",\r\n" + "    \"content\": \"hmtl\",\r\n"
            + " \"summaryContent\": \"null\",\r\n" + "  \"publishedDate\": \"12122018\",\r\n"
            + " \"harvestDate\": \"12122018\",\r\n" + " \"languageId\": \"3\",\r\n" + " \"regionId\": \"45\",\r\n"
            + " \"postStatus\": \"1\"\r\n" + "}";

    ObjectMapper mapper = new ObjectMapper();

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

    System.out.println("Value for getId  is: " + details.getId());
    System.out.println("Value for getSourceApplication  is: " + details.getSourceApplication());
    System.out.println("Value for getExternalId  is: " + details.getPublishedDate());
    System.out.println("Value for getExternalLink  is: " + details.getExternalLink());

} }

**MyPojo.class**

public class MyPojo {
private String content;

private String id;

private String sourceApplication;

private String title;

private String postStatus;

private String publishedDate;

private String summaryContent;

private String harvestDate;

private String languageId;

private String externalId;

private String regionId;

private String externalLink;

public String getContent() {
    return content;
}

public String getId() {
    return id;
}

public String getSourceApplication() {
    return sourceApplication;
}

public String getTitle() {
    return title;
}

public String getPostStatus() {
    return postStatus;
}

public String getPublishedDate() {
    return publishedDate;
}

public String getSummaryContent() {
    return summaryContent;
}

public String getHarvestDate() {
    return harvestDate;
}

public String getLanguageId() {
    return languageId;
}

public String getExternalId() {
    return externalId;
}

public String getRegionId() {
    return regionId;
}

public String getExternalLink() {
    return externalLink;
} }


**RESULT:**
 Value for getId  is: 168
 Value for getSourceApplication  is: adsense
 Value for getExternalId  is: 12122018
 Value for getExternalLink  is: www.gmail.com

注意 必须更改json中的字段以小写字母开头。进行JSON更改的原因是,杰克逊bean序列化将反映在该类上,并且当它看到getXyz()和setXyz()方法会将它们映射到Json归档的名称“ xyz”(而不是“ Xyz”)。认为有多种方法可以替代此行为,一种方法是使用Jackson注释中的一种。

答案 3 :(得分:-1)

您无需创建许多公共获取器,而只需将private变量修改为public