意外的输入结束:在[来源:{; line:1,column:3]

时间:2018-02-27 18:38:26

标签: java json jackson

我需要使用Jackson反序列化json对象 经过多次努力和互联网搜索,所有努力都要解决错误:

Exception in thread "main" com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: {; line: 1, column: 1])
     at [Source: {; line: 1, column: 3]

失败了。

我试了JacksonMixInAnnotations但没有成功。

json的一个例子是

{
    "imageDigest": "sha256:7cc1145883e4e6741fd4f02b8d01636ac341038de51e7923b9a5acf98329602a", 
    "vulnerabilities": [
        {
            "fix": "None", 
            "package": "apt-1.0.9.8.4", 
            "severity": "Negligible", 
            "url": "https://security-tracker.debian.org/tracker/CVE-2011-3374", 
            "vuln": "CVE-2011-3374"
        }
    ], 
    "vulnerability_type": "os"
}

我的反序列化代码:

String line = "";
    File file = new File ("C:\\xxxxxxx\\xxxxxx\\kbastani-movie-microservice04.json");
    Anchore anchore = new Anchore();
    ObjectMapper mapper = new ObjectMapper();
    BufferedReader breader = new BufferedReader(new FileReader(file));
    while((line= breader.readLine()) != null) {
    anchore = mapper.readValue(line, Anchore.class);
    System.out.println(anchore);
    List <Vulnerability> vulnerability = anchore.getVulnerabilities();
    System.out.println(vulnerability.get(1));

    }

    breader.close();

Anchore class

public class Anchore {
    @JsonProperty("imageDigest")
    private String imageDigest;
    @JsonProperty("vulnerabilities")
    private List<Vulnerability> vulnerabilities = null;
    @JsonProperty("vulnerability_type")
    private String vulnerabilityType;

    public Anchore() {
    }

     public Anchore(String imageDigest, List<Vulnerability> vulnerabilities, String vulnerabilityType) {
        super();
        this.imageDigest = imageDigest;
        this.vulnerabilities = vulnerabilities;
        this.vulnerabilityType = vulnerabilityType;
    }
}

漏洞类

@JsonIgnoreProperties(ignoreUnknown = true)
public class Vulnerability {

        @JsonProperty("fix")
        private String fix;
        @JsonProperty("package")
        private String imagePackage;
        @JsonProperty("severity")
        private String severity;
        @JsonProperty("url")
        private String url;
        @JsonProperty("vuln")
        private String vuln;

        public Vulnerability() {
        }
}

2 个答案:

答案 0 :(得分:2)

当你致电breader.readLine()时,你读了一行。 你需要在调用mapper.readValue(line, Anchore.class);之前读取整个json,这应该在循环之外。

  • 将整个文件读成字符串
  • 使用ObjectMapper将字符串转换为您的对象。

编辑:读取文件:

String path = "C:\\xxxxxxxxxx\\xxxxxxxxxx\\kbastani-movie-microservice04.json";
byte[] encoded = Files.readAllBytes(Paths.get(path));
String json = new String(encoded, StandardCharsets.UTF_8);
anchore = mapper.readValue(json, Anchore.class);

答案 1 :(得分:1)

你几乎没有错误

  • 在将json解析为对象之前,需要读取所有文件
  • 当您的json中只有一个vulnerability.get(1)时,您试图获得vulnerability

这是一个简单的&amp;更清洁的方式来实现您的目标:

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


public class MainJackson {
    public static void main(String[] args) throws IOException {

        Path jsonPath = Paths
                .get("jsonPath.json")
                .toAbsolutePath();

        String jsonText = Files.lines(jsonPath)
                .reduce("", String::concat);


        ObjectMapper mapper = new ObjectMapper();

        Anchore anchore = mapper.readValue(jsonText, Anchore.class);
        System.out.println(anchore);

        List<Vulnerability> vulnerability = anchore.getVulnerabilities();
        System.out.println(vulnerability.get(0));

    }
}