x-treme x-treme - 3 months ago 63x
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


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).