Parse JSON array response using Retrofit & Gson

Please try to use this one

    @FormUrlEncoded
    @POST("api/sponsors")
    Call<List<SponsorsResult>> getStatesAndDistrict(
            @Field("xyz") String field1
    );



   Call <List<SponsorsResult>> call = service.getSponsorsValue();

    call.enqueue(new Callback<List<SponsorsResult>>() {
        @Override
        public void onResponse(Call<List<SponsorsResult>> call, Response<List<SponsorsResult>> response) {

            List<SponsorsResult> rs = response.body();

        }

        @Override
        public void onFailure(Call<List<SponsorsResult>> call, Throwable t) {

        }
    });



 class SponsorsResult {

    @SerializedName("sponsors")
    private List<SponsorsValue> sponsors;

    public List<SponsorsValue> getSponsors() {
        return sponsors;
    }
}

class SponsorsValue{
    @SerializedName("leg_id")
    @Expose
    private String legId;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("name")
    @Expose
    private String name;

    public String getLegId() {
        return legId;
    }

    public void setLegId(String legId) {
        this.legId = legId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Please let me know if you are facing any issue.


Create below class in your model package:

 import java.util.List;
 import com.google.gson.annotations.Expose;
 import com.google.gson.annotations.SerializedName;

public class SponsorModel {

 @SerializedName("sponsors")
@Expose
private List<Sponsor> sponsors = null;
@SerializedName("id")
@Expose
private String id;

public List<Sponsor> getSponsors() {
    return sponsors;
}

public void setSponsors(List<Sponsor> sponsors) {
    this.sponsors = sponsors;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

-----------------------------------Sponsor.java-----------------------------------

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Sponsor {

@SerializedName("leg_id")
@Expose
private String legId;
@SerializedName("type")
@Expose
private String type;
@SerializedName("name")
@Expose
private String name;
@SerializedName("committee_id")
@Expose
private Object committeeId;
@SerializedName("official_type")
@Expose
private String officialType;

public String getLegId() {
    return legId;
}

public void setLegId(String legId) {
    this.legId = legId;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Object getCommitteeId() {
    return committeeId;
}

public void setCommitteeId(Object committeeId) {
    this.committeeId = committeeId;
}

public String getOfficialType() {
    return officialType;
}

public void setOfficialType(String officialType) {
    this.officialType = officialType;
}

}

Set above SponsorsModel in your result.

Thanks


The short answer is. if your JSON response starts with a JSON array, use a List:

@GET("bills/")
Call<List<YourResponse>> getListOfSponsors(....);

vs wrong:

@GET("bills/")
Call<YourResponse> getListOfSponsors(....);

You can create POJO classes from json response automatically using this link -> jsonschema2pojo

All you need to do is paste the JSON response in the edit box given.

  1. Select Target language as JAVA,
  2. Source type as JSON
  3. Annotation style as Gson
  4. and check Use primitive types , include getters and setters, allow additional properties