Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输入意外结束:在[Source:{;line:1,column:1]处的对象(起始标记在[Source:{;line:1,column:3]处)应为关闭标记(起始标记在[Source:{;line:1,column:1])_Java_Json_Jackson - Fatal编程技术网

Java 输入意外结束:在[Source:{;line:1,column:1]处的对象(起始标记在[Source:{;line:1,column:3]处)应为关闭标记(起始标记在[Source:{;line:1,column:1])

Java 输入意外结束:在[Source:{;line:1,column:1]处的对象(起始标记在[Source:{;line:1,column:3]处)应为关闭标记(起始标记在[Source:{;line:1,column:1]),java,json,jackson,Java,Json,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:

我需要使用反序列化
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]
我们失败了

我试过了,但没有成功

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();

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

你几乎没有错误

  • 在将json解析为对象之前,需要读取all文件
  • 您正在尝试获取
    漏洞。当json中只有一个
    漏洞时,获取(1)
这里有一个简单而干净的方法来实现您的目标:

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));

    }
}
import com.fasterxml.jackson.databind.ObjectMapper;
导入java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.List;
公共课{
公共静态void main(字符串[]args)引发IOException{
路径jsonPath=路径
.get(“jsonPath.json”)
.toAbsolutePath();
字符串jsonText=Files.lines(jsonPath)
.reduce(“,字符串::concat);
ObjectMapper mapper=新的ObjectMapper();
Anchore Anchore=mapper.readValue(jsonText,Anchore.class);
系统输出打印(锚地);
列表漏洞=anchore.getVulnerabilities();
System.out.println(漏洞.get(0));
}
}

即使遵循您的建议,仍然会出现相同的错误:
while((line=breader.readLine())!=null){
myString=line;}
anchore=mapper.readValue(myString,anchore.class);
漏洞=anchore.getVulnerabilities();
for(漏洞漏洞漏洞漏洞漏洞漏洞:漏洞){
System.out.println(vulns.getFix());}
就像我说的,你需要阅读整个文件,不仅仅是第一行(getLine就是这么做的),我更新了我的答案。哇,它工作得很好。谢谢:)。当你提到“循环”时,我误解了你.所以我基本上不需要循环,这对我来说是新的。谢谢你的另一课。这也非常有效。谢谢,不幸的是我已经接受了@Syl的答案。但是你今天也教了我一些新的东西。谢谢。
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);
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));

    }
}