gpt4 book ai didi

java - 尝试读取 json 文件时出现 JsonParserException

转载 作者:行者123 更新时间:2023-11-29 08:23:48 25 4
gpt4 key购买 nike

书.json

[{
"title": "Hunger Games",
"description": " fast paced In a not-too-distant future, the United States of America has collapsed, weakened by drought, fire, famine, and war, to be replaced by Panem, a country divided into the Capitol and 12 districts. Each year, two young representatives from each district are selected by lottery to participate in The Hunger Games. Part entertainment, part brutal intimidation of the subjugated districts, the televised games are broadcast throughout Panem as the 24 participants are forced to eliminate their competitors, literally, with all citizens required to watch. When 16-year-old Katniss s young sister, Prim, is selected as the mining district s female representative, Katniss volunteers to take her place. She and her male counterpart, Peeta, the son of the town baker who seems to have all the fighting skills of a lump of bread dough, will be pitted against bigger, stronger representatives who have trained for this their whole lives. Collins s characters are completely realistic and sympathetic as they fight and form alliances literary distant future great man and friendships in the face of distant future overwhelming odds; the plot is tense, dramatic, and engrossing. This book will definitely resonate with the generation raised on reality shows like Survivor and American Gladiator. Book one of a planned trilogy."

}]

public class FileReader {

public static void main(String[] args) {
// TODO Auto-generated method stub
String keywordCsv= "resources/addresses.csv";
String booksJson ="resources/books.json";
//Csvfilereader.processBySplit(fileName);
Csvfilereader csvReader= new Csvfilereader();
//ReadJson readjson =new ReadJson();
ObjectMapper csvobj;
ObjectMapper descriptionJsonobj=new ObjectMapper() ;
MapType type = descriptionJsonobj.getTypeFactory().constructMapType(
Map.class, String.class, Object.class);
try {
csvobj= csvReader.processByStreamApi(keywordCsv);
Map<String,Object> jsonMap = descriptionJsonobj.readValue(booksJson, type);
jsonMap.forEach((k,v) -> System.out.println((k +": "+ v)));
}
catch(Exception e) {
e.printStackTrace();

}

System.out.println("Successfull");
//JsonObjectFormatVisitor

}

运行程序后出现异常

    com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'resources': was expecting ('true', 'false' or 'null')
at [Source: (String)"resources/books.json"; line: 1, column: 10]
Successfull
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1899)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:757)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4141)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3042)
at com.readcsv.FileReader.main(FileReader.java:28)

我是java新手。我必须将 csv 文件中的 keword 与 books.json 文件进行匹配。我正在使用 jackson ObjectMapper。

最佳答案

String booksJson ="resources/books.json";

只是一个字符串,可能代表您的文件路径。您正在将此字符串传递给对象映射器 - 而此字符串不是有效的 json。您正在使用的 ObjectMapper 的 readValue 方法期望第一个参数是有效的 json。

这里

descriptionJsonobj.readValue(booksJson, type);

bookJson 应该是一个有效的 json,但目前不是,因为您为其分配了值“resources/books.json”。

您应该将文件内容加载到某个字符串变量中,例如使用 Java 8 Files 类或其他。然后,当您将文件内容加载到某个字符串变量中时,只需将其传递给 ObjectMapper 的 readValue 方法即可。

关于java - 尝试读取 json 文件时出现 JsonParserException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55200395/

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