Parse JSON array response using Retrofit & Gson

前端 未结 4 1646
青春惊慌失措
青春惊慌失措 2020-11-27 18:53

Here is my JSONArray Response from Web service:

[
  {
    "sponsors": [
      {
        "leg_id": "NYL000067",
        "typ         


        
相关标签:
4条回答
  • 2020-11-27 19:21

    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
    0 讨论(0)
  • 2020-11-27 19:30

    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.

    0 讨论(0)
  • 2020-11-27 19:31

    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

    0 讨论(0)
  • 2020-11-27 19:39

    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(....);
    
    0 讨论(0)
提交回复
热议问题