简体   繁体   中英

Error com.fasterxml.jackson.core.JsonParseException: Unrecognized token

Trying to read a JSON file and serialize it to java object, I wrote a method:

public static PostPojo readFile(String titleFile){
    String pathJSONFile = "src/main/resources/"+titleFile+".json";
    ObjectMapper objectMapper = new ObjectMapper();

    try {
         objectMapper.readValue(pathJSONFile,PostPojo.class);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return postPojo;
}

but it produces an error:

    com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'src': was expecting (JSON 
    String, Number, Array, Object or token 'null', 'true' or 'false')
    at [Source: (String)"src/main/resources/ninetyNinthPost.json"; line: 1, column: 4]
    at utils.ApiUtils.readFile(ApiUtils.java:71)
    at ApiApplicationRequest.getValue(ApiApplicationRequest.java:31)

My JSON file from which values are calculated

[ {
 "userId" : 10,
 "id" : 99,
 "title" : "temporibus sit alias delectus eligendi possimus magni",
 "body" : "quo deleniti praesentium dicta non quod\naut est 
 molestias\nmolestias et officia quis nihil\nitaque dolorem quia"
} ]

My java object class

public class PostPojo {

private int userId;
private int id;
private String title;
private String body;
public PostPojo() {
}

public PostPojo(int userId, int id, String title, String body) {
    this.userId = userId;
    this.id = id;
    this.title = title;
    this.body = body;
}

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getBody() {
    return body;
}

public void setBody(String body) {
    this.body = body;
}


@Override
public String toString() {
    return "PostModel{" +
            "userId=" + userId +
            ", id=" + id +
            ", title='" + title + '\'' +
            ", body='" + body + '\'' +
            '}';
}
}

I really don't understand what is the reason.As I understand it, reading in the documentation, it should read the file and present it in the java class. Any sugestions?

There is no method signature supposed to get a file path as first argument. You may pass a JSON String as first argument or you could use the method signature with a File Object as first argument, like this:

public static PostPojo[] readFile(String titleFile){
    String pathJSONFile = "src/main/resources/"+titleFile+".json";
    ObjectMapper objectMapper = new ObjectMapper();
    File jsonFile = new File(pathJSONFile);

    PostPojo[] postPojo = null;
    try {
        postPojo = objectMapper.readValue(jsonFile, PostPojo[].class);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return postPojo;
}

EDIT: Since your file defines a wrapping array around the object you have to parse it as array. Afterwards you may return it as an array like i did in my edited answer or you just return the first array record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM