【发布时间】:2021-01-10 13:13:42
【问题描述】:
我想使用带有 jakson 注释的 POJOS 创建这个 JSON。当我创建一个没有 @JsonProperty 注释的新类来表示最后一个 {"id":"123ccc","role":"dddd"} 时遇到的问题,它默认采用类名并创建类似“customer”的东西:{"id":"123ccc","role":"dddd"}.
我要构建的 JSON 结构
{
"relatedParty": [
{
"contact": [
{
"mediumType": "xxx",
"characteristic": {
"city": "xxx",
"country": "xxx"
}
},
{
"mediumType": "yyy",
"characteristic": {
"emailAddress": "yyy@yy.yyy"
}
}
],
"role": "ccc",
"fullName": "ccc"
},
{
"id": "123ccc",
"role": "dddd"
}
]
}
我从以下代码收到的 JSON。
{
"relatedParty": [
{
"contact": [
{
"mediumType": "xxx",
"characteristic": {
"city": "xxx",
"country": "xxx"
}
},
{
"mediumType": "yyy",
"characteristic": {
"emailAddress": "yyy@yy.yyy"
}
}
],
"role": "ccc",
"fullName": "ccc"
},
"customer" : {
"id": "123ccc",
"role": "dddd"
}
]
}
获取确切的 JSON 格式作为图像的解决方法是什么。当前的实现如下。
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public class RelatedParty {
@JsonProperty(value = "contact")
private List<Contact> contact;
@JsonProperty(value = "role")
private String role;
@JsonProperty(value = "fullName")
private String fullName;
private Customer customer;
public List<Contact> getContact() {
return contact;
}
public void setContact(List<Contact> contact) {
this.contact = contact;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
public class Customer {
@JsonProperty(value = "id")
private String id;
@JsonProperty(value = "role")
private String role;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
【问题讨论】:
标签: java json annotations jaxb