gpt4 book ai didi

java - 使用 Jackson ObjectIdGenerator 将 Json 转换为集合时出现问题

转载 作者:行者123 更新时间:2023-12-02 08:41:48 25 4
gpt4 key购买 nike

我正在尝试将以下 JSON 转换为 Java 中的集合

"films" : [
{
"fid" : 1,
"title" : "The Platform",
"year": 2019,
"genre": [1,2,3],
"director": 1,
"cast": [2,3,4] ,
"description": "A vertical prison with one cell per level. Two people per cell. One only food platform and two minutes per day to feed from up to down. An endless nightmare trapped in The Hole."
}
],

"tvseries" : [
{
"tid" : 1,
"title" : "Breaking Bad",
"year": 2008,
"genre": [9,5,3],
"creator": 13,
"cast": [14,15,16] ,
"description": " A high school chemistry teacher diagnosed with inoperable lung cancer turns to manufacturing and selling methamphetamine in order to secure his family's future."
}
],
"people" : [
{ "pid": 1, "name" : "Galder Gaztelu-Urrutia" }
],

"genres" : [
{ "gid": 1, "genre": "Horror" }
]

以上只是部分数据的示例。我的特殊问题是 Film 类,特别是以下错误:“反序列化属性“director”时出现问题(预期类型:[简单类型,类赋值4.model.Person];实际类型:assignment4.model.Film)"

这是我的电影和人物的类结构:

public abstract class Media {
private String title;
private int year;
private String description;
private ArrayList<Genre> genre = new ArrayList<Genre>();
private ArrayList<Person> cast = new ArrayList<Person>();
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "fid")
public class Film extends Media {
private int fid;
private Person director;

@Override
public int hashCode() {
return Arrays.hashCode(new Object[] { getFid(), getTitle() });
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "pid")
public class Person {
private int pid;
private String name;

@Override
public int hashCode() {
return Arrays.hashCode(new Object[] { getPid(), getName() });
}

public class Collection {

private Set<Film> films = new TreeSet<Film>();
private Set<Genre> genres = new TreeSet<Genre>();
private Set<Person> people = new TreeSet<Person>();
private Set<Profile> profiles = new TreeSet<Profile>();
private Set<TV> tvseries = new TreeSet<TV>();
}

所有类都有用于各种属性的 getter 和 setter。我已经尝试了所有我能想到的方法,但无法弄清楚为什么它无法获得该电影的导演。

最佳答案

您应该在 POJO @JsonIdentityInfo 注释中设置 scope 属性:

@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "pid", scope = Person.class)
public class Person {
private int pid;
private String name;
// getters/setters/hashcode, etc. ...
}

@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "gid", scope = Genre.class
)
public class Genre {
private int gid;
private String genre;
// getters/setters/hashcode, etc. ...
}

@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "fid", scope = Film.class
)
public class Film extends Media {
private int fid;
private String title;
private Person director;
// getters/setters/hashcode, etc. ...
}

关于java - 使用 Jackson ObjectIdGenerator 将 Json 转换为集合时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61343738/

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