Java 尝试读取json文件时出现JsonParserException

Java 尝试读取json文件时出现JsonParserException,java,json,jackson,Java,Json,Jackson,Book.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

Book.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."
}]

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

String booksJson ="resources/books.json";
只是一个字符串,它可能表示文件的路径。您正在将此字符串传递给对象映射器,而此字符串不是有效的json。您正在使用的ObjectMapper的readValue方法期望第一个参数是有效的json

这里

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

您应该将文件内容加载到一些字符串变量中,例如使用Java8Files类或其他任何类型。然后,当您将文件内容加载到某个字符串变量中时,只需将其传递给ObjectMapper的readValue方法

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

这里

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


您应该将文件内容加载到一些字符串变量中,例如使用Java8Files类或其他任何类型。然后,当您将文件内容加载到某个字符串变量中时,只需将其传递给ObjectMapper的readValue方法。

是的,我必须在ObjectMapper中使用新文件,谢谢。我错过了是的,我必须在objectmapper中使用新文件,谢谢。我错过了
    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)
String booksJson ="resources/books.json";
descriptionJsonobj.readValue(booksJson, type);