问题
I'm trying to serialize the following POJO to JSON using Gson:
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;
}
}
However I get the following error:
java.lang.IllegalArgumentException: class com.companyname.mobile.dto.Beneficiary declares multiple JSON fields named benefits
The Beneficiary class looks like this:
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;
}
}
Is there anyway to get past this? I would've thought that Gson would simply take Arrays and ArrayList properties of classes and just convert it into JSON arrays e.g.
{
'benefits' : [
'benefit1' : {...},
'benefit2' : {...},
'benefit3' : {...},
]
}
Thanks in advance for any help and pointers!
回答1:
private ArrayList<Benefit> benefits;
exist twice: in super class Member and Beneficiary.
Remove the declaration in Beneficiary to get rid of the error.
来源:https://stackoverflow.com/questions/33345170/gson-class-declares-multiple-json-fields-named-property