我正在尝试使用 JSONB 反序列化 JSON 数组。
JSON
[
{
"id": "1",
"animal": "dog",
"age": "3"
},
{
"id": "2",
"animal": "cat",
"age": "5"
}
]
控制器
Jsonb jsonb = JsonbBuilder.create();
Animal animal;
AnimalsList animalsList;
public AnimalsList getAnimals() {
try {
animalsList = jsonb.fromJson("[{\"id\":\"1\",\"animal\":\"dog\",\"age\":\"3\"},{\"id\":\"2\",\"animal\":\"cat\",\"age\":\"5\"}]", AnimalsList.class);
} catch (JSONException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
return animalsList;
}
动物清单
public class AnimalsList implements Serializable{
private List<Animal> list;
public AnimalsList() {
}
public AnimalsList(List<Animal> list) {
this.list = list;
}
// getter & setter
}
动物
public class Animal implements Serializable{
private int id;
private String animal;
private int age;
public Animal() {
}
public Animal(int id, String animal, int age) {
this.id = id;
this.animal = animal;
this.age = age;
}
// getter & setter
}
但我收到以下错误:
javax.json.bind.JsonbException: Can't deserialize JSON array into: class com.model.AnimalsList
红糖糍粑
红颜莎娜
相关分类