尝试读取json文件时出现JsonParserException

问题描述 投票:0回答:1

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文件中的关键字与books.json文件匹配。我正在使用jackson ObjectMapper。

java json jackson
1个回答
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方法即可。

© www.soinside.com 2019 - 2024. All rights reserved.