我建立了一个Google Calendar API,但我想念我的json文件。
我成功使用json文件创建了Java对象,但这是问题所在:
我有两个班级:
public class User {
private String email;
private String firstname;
private String lastname;
Entity entity;
``
and my Entity
`` public class Entity {
private String name;
private String entityType;
private Entity rootEntity;``
here my json file :
for user
``[
{
"firstname": "Jean-Marc",
"lastname": "Chevereau",
"email": "xxxxxxx@xxxxx.com",
"entity": {
"name":"BFA",
"entityType":"secteur"
}
},
{
"firstname": "Florent",
"lastname": "Hamlin",
"email": "xxxxxxx@xxxxx.com",
"entity": {
"name":"IT",
"entityType":"secteur"
}
},
{
"firstname": "Benoit",
"lastname": "Micaud",
"email": "xxxxxxx@xxxxx.com",
"entity": {
"name":"EX",
"entityType":"offre",
"rootEntity":{
"name":"BFA"
}
}
}
]``
And a Entity json file
```[
{
"name": "BFA",
"entityType": "secteur",
"rootEntity": "",
},
{
"name": "EX",
"entityType": "Offre",
"rootEntity": "BFA",
}
}
]
但是这里很麻烦。如果在我的User.json中我写了实体名称,我不想写entitytype和rootEntity,因为如果我写的实体名称是BFA,它将始终是相同的entitType和rootEntity。 换句话说,我的json实体将始终是相同的,并且如果我只输入我们知道的指向实体对象的名称。
例如,在此user.json文件中,我只需要放入
[
{
"firstname": "Jean-Marc",
"lastname": "Chevereau",
"email": "xxxxxxx@xxxxx.com",
"entity": {
"name":"BFA",
}
},
{
"firstname": "Florent",
"lastname": "Hamlin",
"email": "xxxxxxx@xxxxx.com",
"entity": {
"name":"IT",
}
},
{
"firstname": "Benoit",
"lastname": "Micaud",
"email": "xxxxxxx@xxxxx.com",
"entity": {
"name":"EX",
}
}
]
答案 0 :(得分:0)
我认为com.fasterxml.jackson的@JsonIgnore批注应该有帮助。
public class Entity {
private String name;
@JsonIgnore
private String entityType;
@JsonIgnore
private Entity rootEntity;
}
答案 1 :(得分:0)
在Json-lib中,您有一个JsonConfig
用于指定允许的字段:
JsonConfig jsonConfig=new JsonConfig();
jsonConfig.registerPropertyExclusion(Entity.class,"rootEntity");
jsonConfig.registerPropertyExclusion(Entity.class,"entityType");
JSON json = JSONSerializer.toJSON(objectToWrite,jsonConfig);