gpt4 book ai didi

java - 意外的输入结束 : expected close marker for Object (start marker at [Source: {; line: 1, 列 : 1]) at [Source: {; line: 1, 列:3]

转载 作者:行者123 更新时间:2023-11-29 04:21:16 34 4
gpt4 key购买 nike

我需要使用 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();

anchor 类

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() {
}
}

最佳答案

当您调用 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);

关于java - 意外的输入结束 : expected close marker for Object (start marker at [Source: {; line: 1, 列 : 1]) at [Source: {; line: 1, 列:3],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49015718/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com