[英]Error com.fasterxml.jackson.core.JsonParseException: Unrecognized token
Trying to read a JSON file and serialize it to java object, I wrote a method:尝试读取 JSON 文件并将其序列化为 java 对象,我编写了一个方法:
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我的 JSON 文件,从中计算值
[ {
"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我的java对象类
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.我真的不明白是什么原因。据我所知,阅读文档,它应该读取文件并将其呈现在java类中。 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:
您可以将 JSON 字符串作为第一个参数传递,也可以使用带有文件对象的方法签名作为第一个参数,如下所示:
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.
之后,您可以像我在编辑的答案中所做的那样将其作为数组返回,或者您只返回第一个数组记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.