我正在尝试使用Gson将以下POJO序列化为JSON:
public class Member {
private long id;
private long customerAccountNumber;
private long memberNumber;
private String title;
private String initials;
private String telephoneNumber;
private String maritalStatus;
private String firstName;
private String surname;
private Date birthDate;
private Date joinDate;
private String gender;
private String language;
private String idNumber;
private String passportNumber;
private String mobileNumber;
private String emailAddress;
private String fingerprintMinutia;
private String status;
private String pinNumber;
private String commsConsent;
private String homeTelNumber;
private String workTelNumber;
private String faxNumber;
private String otherTelNumber;
private Address address;
private BankAccount bankAccount;
private Product product;
private Society society;
private ArrayList<Beneficiary> beneficiaries;
private ArrayList<Benefit> benefits;
public String getCommsConsent() {
return commsConsent;
}
public void setCommsConsent(String commsConsent) {
this.commsConsent = commsConsent;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getInitials() {
return initials;
}
public void setInitials(String initials) {
this.initials = initials;
}
public String getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(String telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public String getMaritalStatus() {
return maritalStatus;
}
public void setMaritalStatus(String maritalStatus) {
this.maritalStatus = maritalStatus;
}
public String getPinNumber() {
return pinNumber;
}
public void setPinNumber(String pinNumber) {
this.pinNumber = pinNumber;
}
public ArrayList<Benefit> getBenefits() {
return benefits;
}
public void setBenefits(ArrayList<Benefit> benefits) {
this.benefits = benefits;
}
public ArrayList<Beneficiary> getBeneficiaries() {
return beneficiaries;
}
public void setBeneficiaries(ArrayList<Beneficiary> beneficiaries) {
this.beneficiaries = beneficiaries;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public BankAccount getBankAccount() {
return bankAccount;
}
public void setBankAccount(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getCustomerAccountNumber() {
return customerAccountNumber;
}
public void setCustomerAccountNumber(long customerAccountNumber) {
this.customerAccountNumber = customerAccountNumber;
}
public long getMemberNumber() {
return memberNumber;
}
public void setMemberNumber(long memberNumber) {
this.memberNumber = memberNumber;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public String getPassportNumber() {
return passportNumber;
}
public void setPassportNumber(String passportNumber) {
this.passportNumber = passportNumber;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getFingerprintMinutia() {
return fingerprintMinutia;
}
public void setFingerprintMinutia(String fingerprintMinutia) {
this.fingerprintMinutia = fingerprintMinutia;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Society getSociety() {
return society;
}
public void setSociety(Society society) {
this.society = society;
}
public String getHomeTelNumber() {
return homeTelNumber;
}
public void setHomeTelNumber(String homeTelNumber) {
this.homeTelNumber = homeTelNumber;
}
public String getWorkTelNumber() {
return workTelNumber;
}
public void setWorkTelNumber(String workTelNumber) {
this.workTelNumber = workTelNumber;
}
public String getFaxNumber() {
return faxNumber;
}
public void setFaxNumber(String faxNumber) {
this.faxNumber = faxNumber;
}
public String getOtherTelNumber() {
return otherTelNumber;
}
public void setOtherTelNumber(String otherTelNumber) {
this.otherTelNumber = otherTelNumber;
}
}
但是我收到以下错误:
java.lang.IllegalArgumentException:class com.companyname.mobile.dto.Beneficiary声明多个JSON字段 命名利益
受益人类看起来像这样:
public class Beneficiary extends Member {
private String type;
private Date terminationDate;
private ArrayList<Benefit> benefits;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Date getTerminationDate() {
return terminationDate;
}
public void setTerminationDate(Date terminationDate) {
this.terminationDate = terminationDate;
}
public ArrayList<Benefit> getBenefits() {
return benefits;
}
public void setBenefits(ArrayList<Benefit> benefits) {
this.benefits = benefits;
}
}
无论如何都要过去吗?我会认为Gson只需要获取类的Arrays和ArrayList属性,然后将其转换为JSON数组,例如
{
'benefits' : [
'benefit1' : {...},
'benefit2' : {...},
'benefit3' : {...},
]
}
提前感谢您的任何帮助和指示!
答案 0 :(得分:4)
private ArrayList<Benefit> benefits;
存在两次:超级会员和受益人。
删除受益人中的声明以消除错误。