x-treme x-treme - 1 year ago 243
Java Question

GSON @SerializedName to get value from inner array in json

This is my json

name: "mark"
subject: "maths"
phone: 123-456-7890
email_addresses: [ { email: "mark@example.com", is_primary: true } ]

My java class goes like this

public class Student {
@SerializedName("name") private String mName;
@SerializedName("subject") private String mSubject;
@SerializedName("phone") private String mPhone;
private String mEmail;

Is there a way for to use @SerializedName for mEmail, so that I would be able to get the email field from the first object in the email_addresses array

Answer Source

No, there isn't. Either create your own TypeAdapter or create a POJO type for email addresses and have Student declare a field of type List of whatever that POJO type is. Provider a getter to only retrieve the first email (if there is one).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download