简体   繁体   English

使用Jackson将JSON映射到POJO

[英]Map JSON to POJO Using Jackson

I'm trying to get map a JSON to a POJO using jackson and I keep getting the following error: 我正在尝试使用杰克逊将JSON映射到POJO,并且不断收到以下错误:

> Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: (String)\"{\"checkstyle\

The JSON Im trying to parse is the following: 尝试解析的JSON Im如下:

{
  "checkstyle": {
    "file": [
      {
        "name": "src\\main\\java\\com\\report\\uploader\\controller\\RandomController.java",
        "error": [
          {
            "severity": "error",
            "line": 0,
            "source": "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
            "message": "Missing package-info.java file."
          }
        ]
      }
    ],
    "version": 6.18
  }
}

For this I create the following classes: 为此,我创建以下类:

public class Checkstyle {

    @JsonProperty("checkstyle")
    private Linter linterName;
}

public class Linter {

    @JsonProperty("file")
    private List<File> files;

    @JsonProperty("version")
    private String version;
}

public class File {

    @JsonProperty("name")
    private String name;

    @JsonProperty("error")
    private List<Error> errores;

}

public class Error {

    @JsonProperty("severity")
    private String severity;

    @JsonProperty("line")
    private int line;

    @JsonProperty("source")
    private String source;

    @JsonProperty("message")
    private String message;
}

But when I run the code I get the error mentioned above. 但是,当我运行代码时,出现了上面提到的错误。 The way I get this JSON is by converting an XML file to a JSONObject using the org.json dependency and then the JSONobject I convert it to a String. 我获得此JSON的方法是使用org.json依赖项将XML文件转换为JSONObject,然后将JSONobject转换为String。

Then I convert then I try to convert the String into my POJO the following way: 然后我进行转换,然后尝试通过以下方式将String转换为POJO:

ObjectMapper mapper = new ObjectMapper();
Checkstyle checkstyle = mapper.readValue(object.toString(), Checkstyle.class);

If anyone could point me out what I'm doing wrong I would appreciate. 如果有人能指出我在做什么错,我将不胜感激。

This is the line causing error in your program mapper.readValue(object.toString(), Checkstyle.class) . 这是在您的程序mapper.readValue(object.toString(), Checkstyle.class)引起错误的行。 You have already read the json into an anonymous object , and then using its toString() representation in ObjectMapper to map to Checkstyle class, which will never work. 您已经将json读入一个匿名object ,然后使用ObjectMapper中的toString()表示形式映射到Checkstyle类,该类将永远无法工作。 As you already have lost the json string into java default toString representation of object: someObjectClassname@hashcodenumber . 由于您已经将json字符串丢失到java对象的默认toString表示形式中: someObjectClassname@hashcodenumber

Below are the some of the commonly used signature of readValue method to do correrct de-serialization: 下面是一些常用的readValue方法签名来进行正确的反序列化:

readValue(InputStream in, Class c) readValue(InputStream in,类c)
readValue(Reader rd, Class c) readValue(Reader rd,Class c)
readValue(String json, Class c) readValue(String json,Class c)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM