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.
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.
- Select Target language as JAVA,
- Source type as JSON
- Annotation style as Gson
- and check Use primitive types , include getters and setters, allow additional properties