当 json 键与 jackson 相同时解析 json
parse json when json keys are the same with jackson
我有 json 具有与此处所示相同的键
{
"tests": [
{
"id": 1,
"object_type": "bla",
"objects": {
"id": 59,
"company_name": "apple",
"project_name": "iphone",
"duration": 145
}
},
{
"id": 66,
"object_type": "gla",
"objects": {
"id": 59,
"institution_name": "Test",
"subject": "Gsks",
"duration": 2
}
}
]
}
当我创建 class
A.class
列出我的班级;
TestClass.class
int id;
String object type;
@JsonProperty("objects")
Company objects1;
@JsonProperty("objects")
Subject objects2;
但是当我想解析它时,Jackson 给我一个无法解析的错误,因为我的 class 有 2 个对象(我的意思是 json 有关键对象,其主体具有不同的键)
注意:我知道当我有一个对象时我可以解析它......但我必须按照上面的解释这样做。
有办法吗?
根据 Sfat 的评论:This 可以是一种方法。
根据我的评论,
我的意思是:
如下所示创建您的 SubCompany
class,其中包含 Company
和 Subject
class.
中的所有字段
public class SubCompany {
//Just so you can ignore in Json
@JsonIgnore
String type;
int id;
// All fields from Company Class
String companyName;
String projectName;
int duration;
//Remaining fields from Subject Class
String institutionName;
String subject;
//Getters and Setters
public SubCompany() {
//At the end call method to test the type
setType();
}
private void setType() {
if (null != institutionName && null != subject)
type = "Subject";
else
type = "Company";
}
//At this point the type is set so you know what you have Company or Subject
//Now write customized writers.
public Company getComany() {
Company company = new Company();
//Set all the fields accordingly.
return company;
}
public Subject getSubject() {
//In the similar fashion, write this method to get Subject.
}
}
当你想在反序列化后检索 Subject
或 Company
对象时,你检查 SubCompany
是什么类型,并根据此获取相关对象.
if(type.equals("Subject")
Subject sub = subCompany.getSubject();
我有 json 具有与此处所示相同的键
{
"tests": [
{
"id": 1,
"object_type": "bla",
"objects": {
"id": 59,
"company_name": "apple",
"project_name": "iphone",
"duration": 145
}
},
{
"id": 66,
"object_type": "gla",
"objects": {
"id": 59,
"institution_name": "Test",
"subject": "Gsks",
"duration": 2
}
}
]
}
当我创建 class
A.class 列出我的班级;
TestClass.class
int id;
String object type;
@JsonProperty("objects")
Company objects1;
@JsonProperty("objects")
Subject objects2;
但是当我想解析它时,Jackson 给我一个无法解析的错误,因为我的 class 有 2 个对象(我的意思是 json 有关键对象,其主体具有不同的键)
注意:我知道当我有一个对象时我可以解析它......但我必须按照上面的解释这样做。
有办法吗?
根据 Sfat 的评论:This 可以是一种方法。
根据我的评论,
我的意思是:
如下所示创建您的 SubCompany
class,其中包含 Company
和 Subject
class.
public class SubCompany {
//Just so you can ignore in Json
@JsonIgnore
String type;
int id;
// All fields from Company Class
String companyName;
String projectName;
int duration;
//Remaining fields from Subject Class
String institutionName;
String subject;
//Getters and Setters
public SubCompany() {
//At the end call method to test the type
setType();
}
private void setType() {
if (null != institutionName && null != subject)
type = "Subject";
else
type = "Company";
}
//At this point the type is set so you know what you have Company or Subject
//Now write customized writers.
public Company getComany() {
Company company = new Company();
//Set all the fields accordingly.
return company;
}
public Subject getSubject() {
//In the similar fashion, write this method to get Subject.
}
}
当你想在反序列化后检索 Subject
或 Company
对象时,你检查 SubCompany
是什么类型,并根据此获取相关对象.
if(type.equals("Subject")
Subject sub = subCompany.getSubject();