gpt4 book ai didi

java - 读取 json 文件并使用 jackson.fasterxml 将其映射到 pojo 时出现 JsonMappingException

转载 作者:行者123 更新时间:2023-12-01 12:46:50 25 4
gpt4 key购买 nike

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

我正在使用 fasterxml.jacksonjson 文件中提取数据,并希望将它们设置在 POJO 中。
这是我的 Json 文件:

{"studentsjson":
[
{
"name": "Henry Lee",
"course": "reading",
"year": "2000",
"grade": "3"
},
{
"name": "John Flex",
"course": "writing",
"year": "2001",
"grade": "4"
}
]
}

我的 pojo 类是:

import java.util.*;
import com.fasterxml.jackson.annotation.*;

@JsonRootName("studentsjson")
public class Students{
List<StudentInfo> studentInfo;
// getter and setter for above property
public Students(){
System.out.println("in Students constructor");
}
} // end of students class

import com.fasterxml.jackson.annotation.*;

public class StudentInfo{
String name;
String course;
int grade;
int yob;
// getters and setters
@Override
public String toString() {
return "Student [name=" + name + ", course=" + course + ", grade=" + grade + ", yob=" + yob + "]";
}

} // end of student class

这是我的主要方法类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonReadJava{

public static void main(String args[])throws JsonParseException, JsonMappingException, IOException
{
try{
Path path = Paths.get("C:/Users/Radhika/Desktop/JavaJson/studentinfo.json.txt");
byte[] fileasbytes = Files.readAllBytes(path);
String JsonAsString = new String(fileasbytes, "UTF-8");
System.out.println("bytes[]asString: "+ JsonAsString);


ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, Students.class);
List<Students> stuList = (ArrayList<Students>)mapper.readValue(JsonAsString, type);

System.out.println("student is:"+stuList);

}
catch(Exception e){
e.printStackTrace();
}
}//end of main
}//end of class

JsonReadJava.java:34: error: cannot find symbol
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, tru
e);
^
symbol: variable Feature
location: class DeserializationConfig
Note: JsonReadJava.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instanc
e of java.util.ArrayList out of START_OBJECT token
at [Source: {"studentsjson":[
{ "name":"Henry Lee", "course":"reading", "year":"2000", "grade":"3"},
{ "name":"John Flex", "course":"writing", "year":"2001", "grade":"4"}
]
}

; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingE
xception.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingExceptio
n(DeserializationContext.java:749)
at com.fasterxml.jackson.databind.DeserializationContext.mappingExceptio
n(DeserializationContext.java:745)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handl
eNonArray(CollectionDeserializer.java:273)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deser
ialize(CollectionDeserializer.java:216)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deser
ialize(CollectionDeserializer.java:206)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deser
ialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMa
pper.java:3051)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.ja
va:2160)
at JsonReadJava.main(JsonReadJava.java:36)

C:\Users\Radhika\Desktop\JavaJson>javac *.java
JsonReadJava.java:34: error: cannot find symbol
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, tru
e);
^
symbol: variable Feature
location: class DeserializationConfig
Note: JsonReadJava.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

最佳答案

以下是恕我直言,对学生进行反序列化的更简单方法:

public class Students {

@JsonProperty(value="studentsjson")
private List<StudentInfo> studentInfo;
.....
}

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Students students = mapper.readValue(JsonAsString, Students.class);
System.out.println("student is:"+students);

关于java - 读取 json 文件并使用 jackson.fasterxml 将其映射到 pojo 时出现 JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24621500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com