尝试读取json文件时发生JsonParserException

时间:2019-03-16 18:52:04

标签: 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 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文件进行匹配。我正在使用杰克逊ObjectMapper。

1 个答案:

答案 0 :(得分:4)

String booksJson ="resources/books.json";

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

这里

descriptionJsonobj.readValue(booksJson, type);

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

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