简体   繁体   English

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

[英]jackson java Unrecognized field not marked as ignorable

The error code :错误代码

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

I have the below JSON file:我有以下 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"
}

and my code is我的代码是

JacksonTester.java: 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() {
    }

}
}

You need to either have setters for those fields or a constructor that accepts those fields as parameters (+ approriate annotations or -parameters from Java 8 and jackson-module-parameter-names module):你需要要么有这些字段或接受这些字段作为参数的构造制定者(+ approriate注释或-parameters从Java 8和杰克逊模块参数名称模块):

public static class Student {
    ...
    String postStatus;   

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

    ...
}

Jackson has no access to the fields of Student. Jackson 无法访问 Student 的字段。

Implement the public getters and setters for Student and it works.为 Student 实现公共 getter 和 setter 并且它有效。

I sorted this problem and it's working fine.我解决了这个问题,它工作正常。 Here is my code for the same.这是我的代码。

 **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

NOTE One has to change the fields in the json to begin with a lower case letter.注意必须将 json 中的字段更改为以小写字母开头。 The reason for the JSON change is that the Jackson bean serialisation will reflect over the class, and when it sees getXyz() and setXyz() methods will map these to a Json filed names "xyz" (and not "Xyz").I think there are several ways to override this behaviour, one is to use the one of the Jackson annotations. JSON 更改的原因是 Jackson bean 序列化将反映在类上,当它看到 getXyz() 和 setXyz() 方法时,会将它们映射到 Json 文件名称“xyz”(而不是“Xyz”)。我认为有几种方法可以覆盖这种行为,一种是使用 Jackson 注释之一。

您可以简单地将private变量修改为public ,而不是创建这么多公共 getter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Jackson-无法识别的字段“ response”在以下位置未标记为可忽略 - Spring Jackson - Unrecognized field \“response\” not marked as ignorable at Jackson Json 反序列化:无法识别的字段“...”,未标记为可忽略 - Jackson Json Deserialisation: Unrecognized field “…” , not marked as ignorable Jackson 与 JSON:无法识别的字段,未标记为可忽略 - Jackson with JSON: Unrecognized field, not marked as ignorable 未被识别的字段,未标记为可忽略 - unrecognized field, not marked as ignorable JSON 到 Java 对象 - 无法识别的字段,未标记为可忽略 - JSON to Java object - Unrecognized field, not marked as ignorable Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“”不可标记 - Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" not marked as ignorable 无法识别的字段类未标记为可忽略 - Unrecognized field class not marked as ignorable JSON:无法识别的字段“值”( <objectClass> ),没有标记为可忽略的 - JSON : Unrecognized field “value” (<objectClass>), not marked as ignorable org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“id”(类标准),未标记为可忽略 - org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field “id” (Class Criteria), not marked as ignorable 将 JSON 映射到 POJO 进行处理 - 无法识别的字段未标记为可忽略 - Map JSON to POJO For Processing - Unrecognized field not marked as ignorable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM