Android 使用改型解析JSON数组响应&;格森

Android 使用改型解析JSON数组响应&;格森,android,json,gson,retrofit2,Android,Json,Gson,Retrofit2,以下是来自Web服务的JSONArray响应: [ { "sponsors": [ { "leg_id": "NYL000067", "type": "primary", "name": "AUBRY" }, { "leg_id"

以下是来自Web服务的JSONArray响应:

[
  {
    "sponsors": [
      {
        "leg_id": "NYL000067",
        "type": "primary",
        "name": "AUBRY"
      },
      {
        "leg_id": "NYL000171",
        "type": "cosponsor",
        "name": "PERRY"
      },
      {
        "leg_id": "NYL000066",
        "type": "cosponsor",
        "name": "ARROYO"
      },
      {
        "leg_id": "NYL000223",
        "type": "cosponsor",
        "name": "BARRETT"
      },
      {
        "leg_id": "NYL000312",
        "type": "cosponsor",
        "name": "STECK"
      },
      {
        "leg_id": "NYL000180",
        "type": "cosponsor",
        "name": "RIVERA"
      },
      {
        "leg_id": "NYL000114",
        "type": "cosponsor",
        "name": "GOTTFRIED"
      },
      {
        "leg_id": "NYL000091",
        "type": "cosponsor",
        "name": "COOK"
      },
      {
        "leg_id": "NYL000126",
        "type": "cosponsor",
        "name": "JAFFEE"
      },
      {
        "leg_id": "NYL000327",
        "type": "cosponsor",
        "name": "DAVILA"
      },
      {
        "leg_id": "NYL000144",
        "type": "cosponsor",
        "name": "LUPARDO"
      },
      {
        "leg_id": null,
        "type": "cosponsor",
        "name": "SEPULVEDA",
        "committee_id": null
      },
      {
        "leg_id": "NYL000290",
        "type": "cosponsor",
        "name": "MOSLEY"
      },
      {
        "leg_id": "NYL000167",
        "type": "cosponsor",
        "name": "ORTIZ"
      },
      {
        "leg_id": "NYL000170",
        "type": "cosponsor",
        "name": "PEOPLES-STOKES"
      },
      {
        "leg_id": "NYL000221",
        "type": "cosponsor",
        "name": "SKARTADOS"
      },
      {
        "leg_id": "NYL000216",
        "type": "cosponsor",
        "name": "QUART"
      },
      {
        "leg_id": "NYL000208",
        "type": "cosponsor",
        "name": "WEPRIN"
      },
      {
        "leg_id": "NYL000186",
        "type": "cosponsor",
        "name": "ROSENTHAL"
      },
      {
        "leg_id": "NYL000139",
        "type": "cosponsor",
        "name": "LIFTON"
      },
      {
        "leg_id": "NYL000064",
        "type": "cosponsor",
        "name": "ABINANTI"
      },
      {
        "leg_id": "NYL000173",
        "type": "cosponsor",
        "name": "PRETLOW"
      },
      {
        "leg_id": "NYL000339",
        "type": "cosponsor",
        "name": "BICHOTTE"
      },
      {
        "leg_id": null,
        "type": "cosponsor",
        "name": "BARRON",
        "committee_id": null
      },
      {
        "leg_id": "NYL000341",
        "type": "cosponsor",
        "name": "WALKER"
      },
      {
        "leg_id": "NYL000355",
        "type": "cosponsor",
        "name": "BLAKE"
      },
      {
        "leg_id": "NYL000185",
        "type": "cosponsor",
        "name": "RODRIGUEZ"
      },
      {
        "leg_id": "NYL000259",
        "type": "cosponsor",
        "name": "FAHY"
      },
      {
        "leg_id": "NYL000203",
        "type": "cosponsor",
        "name": "TITONE"
      },
      {
        "leg_id": "NYL000286",
        "type": "cosponsor",
        "name": "MCDONALD"
      },
      {
        "leg_id": "NYL000081",
        "type": "cosponsor",
        "name": "CAHILL"
      },
      {
        "leg_id": "NYL000421",
        "type": "cosponsor",
        "name": "HARRIS"
      },
      {
        "leg_id": "NYL000338",
        "type": "cosponsor",
        "name": "JOYNER"
      },
      {
        "leg_id": "NYL000448",
        "type": "cosponsor",
        "name": "JENNE"
      },
      {
        "leg_id": "NYL000070",
        "type": "cosponsor",
        "name": "BENEDETTO"
      },
      {
        "leg_id": "NYL000310",
        "type": "cosponsor",
        "name": "SOLAGES"
      },
      {
        "leg_id": "NYL000357",
        "type": "cosponsor",
        "name": "RICHARDSON"
      },
      {
        "leg_id": "NYL000324",
        "type": "cosponsor",
        "name": "PICHARDO"
      },
      {
        "leg_id": "NYL000423",
        "type": "cosponsor",
        "name": "HYNDMAN"
      },
      {
        "leg_id": "NYL000313",
        "type": "cosponsor",
        "name": "STIRPE"
      },
      {
        "leg_id": "NYL000131",
        "type": "cosponsor",
        "name": "KAVANAGH"
      },
      {
        "leg_id": "NYL000204",
        "type": "cosponsor",
        "name": "TITUS"
      },
      {
        "leg_id": "NYL000275",
        "type": "cosponsor",
        "name": "KIM"
      },
      {
        "leg_id": "NYL000195",
        "type": "cosponsor",
        "name": "SIMOTAS"
      },
      {
        "leg_id": "NYL000293",
        "type": "cosponsor",
        "name": "OTIS"
      },
      {
        "leg_id": "NYL000220",
        "type": "cosponsor",
        "name": "MAYER"
      },
      {
        "leg_id": "NYL000344",
        "type": "cosponsor",
        "name": "JEAN-PIERRE"
      }
    ],
    "id": "NYB00099404"
  },
  {
    "sponsors": [
      {
        "leg_id": "NJL000113",
        "type": "primary",
        "name": "Vainieri Huttle, Valerie"
      }
    ],
    "id": "NJB00038817"
  },
  {
    "sponsors": [
      {
        "leg_id": "NYL000208",
        "type": "primary",
        "name": "WEPRIN"
      }
    ],
    "id": "NYB00066288"
  },
  {
    "sponsors": [
      {
        "leg_id": "NJL000083",
        "type": "primary",
        "name": "Jasey, Mila M."
      },
      {
        "leg_id": "NJL000086",
        "type": "primary",
        "name": "Caputo, Ralph R."
      },
      {
        "leg_id": "NJL000046",
        "type": "primary",
        "name": "Watson Coleman, Bonnie"
      },
      {
        "leg_id": "NJL000177",
        "type": "primary",
        "name": "Pintor Marin, Eliana"
      },
      {
        "leg_id": "NJL000164",
        "type": "primary",
        "name": "Sumter, Shavonda E."
      },
      {
        "leg_id": "NJL000104",
        "type": "primary",
        "name": "Oliver, Sheila Y."
      },
      {
        "leg_id": "NJL000095",
        "type": "primary",
        "name": "Mainor, Charles"
      },
      {
        "leg_id": "NJL000165",
        "type": "primary",
        "name": "Wimberly, Benjie E."
      },
      {
        "leg_id": "NJL000016",
        "type": "cosponsor",
        "name": "Fuentes, Angel"
      },
      {
        "leg_id": "NJL000085",
        "type": "cosponsor",
        "name": "Tucker, Cleopatra G."
      },
      {
        "leg_id": "NJL000067",
        "type": "cosponsor",
        "name": "Stender, Linda"
      },
      {
        "leg_id": "NJL000114",
        "type": "cosponsor",
        "name": "Johnson, Gordon M."
      },
      {
        "leg_id": "NJL000082",
        "type": "cosponsor",
        "name": "McKeon, John F."
      },
      {
        "leg_id": "NJL000129",
        "type": "cosponsor",
        "name": "Singleton, Troy"
      }
    ],
    "id": "NJB00030216"
  },
  {
    "sponsors": [
      {
        "leg_id": "NYL000088",
        "official_type": "SPONSOR",
        "type": "primary",
        "name": "Clark"
      },
      {
        "leg_id": "NYL000171",
        "official_type": "COSPNSR",
        "type": "cosponsor",
        "name": "Perry"
      }
    ],
    "id": "NYB00035573"
  },
  {
    "sponsors": [
      {
        "leg_id": "NJL000111",
        "type": "primary",
        "name": "Schaer, Gary S."
      },
      {
        "leg_id": "NJL000018",
        "type": "primary",
        "name": "Lampitt, Pamela R."
      },
      {
        "leg_id": "NJL000105",
        "type": "primary",
        "name": "Giblin, Thomas P."
      },
      {
        "leg_id": "NJL000171",
        "type": "primary",
        "name": "Mosquera, Gabriela M."
      },
      {
        "leg_id": "NJL000011",
        "type": "cosponsor",
        "name": "Moriarty, Paul D."
      },
      {
        "leg_id": "NJL000165",
        "type": "cosponsor",
        "name": "Wimberly, Benjie E."
      },
      {
        "leg_id": "NJL000010",
        "type": "cosponsor",
        "name": "Madden, Fred H."
      },
      {
        "leg_id": "NJL000007",
        "type": "cosponsor",
        "name": "Sweeney, Stephen M."
      },
      {
        "leg_id": "NJL000039",
        "type": "cosponsor",
        "name": "Thompson, Samuel D."
      },
      {
        "leg_id": "NJL000112",
        "type": "cosponsor",
        "name": "Weinberg, Loretta"
      },
      {
        "leg_id": "NJL000087",
        "type": "cosponsor",
        "name": "Ruiz, M. Teresa"
      }
    ],
    "id": "NJB00021785"
  },
  {
    "sponsors": [
      {
        "leg_id": "NYL000138",
        "official_type": "SPONSOR",
        "type": "primary",
        "name": "Lentol"
      }
    ],
    "id": "NYB00010409"
  },
  {
    "sponsors": [
      {
        "leg_id": "NJL000028",
        "type": "primary",
        "name": "Gove, DiAnne C."
      },
      {
        "leg_id": "NJL000027",
        "type": "primary",
        "name": "Rumpf, Brian E."
      },
      {
        "leg_id": "NJL000099",
        "type": "cosponsor",
        "name": "Prieto, Vincent"
      }
    ],
    "id": "NJB00002928"
  },
  {
    "sponsors": [
      {
        "leg_id": "NJL000094",
        "type": "primary",
        "name": "Chiappone, Anthony"
      },
      {
        "leg_id": "NJL000034",
        "type": "cosponsor",
        "name": "Angelini, Mary Pat"
      }
    ],
    "id": "NJB00010952"
  }
]
下面是ApiInterface.java

public interface ApiInterface {

    @GET("bills/")
    Call<SponsorsResult> getListOfSponsors(@Query("bill_id") String billId , @Query("session") String session, @Query("fields") String field);

}

它无法执行响应。请指导我分析此响应。

在模型包中创建以下类:

 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;
}

}
在您的结果中设置以上赞助商模型


谢谢

请试着用这个

    @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;
    }

}
@FormUrlEncoded
@邮政(“api/赞助商”)
呼叫getStatesAndDistrict(
@字段(“xyz”)字符串字段1
);
Call Call=service.getSponsorsValue();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
List rs=response.body();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
类赞助商结果{
@序列化名称(“发起人”)
私人名单赞助商;
公开名单{
返回赞助商;
}
}
类赞助商价值{
@SerializedName(“leg_id”)
@暴露
私人字符串合法;
@序列化名称(“类型”)
@暴露
私有字符串类型;
@序列化名称(“名称”)
@暴露
私有字符串名称;
公共字符串getLegId(){
返回法;
}
public void setLegId(字符串legId){
this.legId=legId;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}

如果您面临任何问题,请告诉我。

您可以使用此链接从json响应自动创建POJO类->

您只需将JSON响应粘贴到给定的编辑框中

  • 选择目标语言为JAVA
  • 源类型为JSON
  • 注释样式为Gson
  • 并检查使用基本类型(包括getter和setter)是否允许附加属性

  • 简单的回答是。如果JSON响应以JSON数组开头,请使用以下列表:

    @GET("bills/")
    Call<List<YourResponse>> getListOfSponsors(....);
    
    @GET(“bills/”)
    调用getListofSposors(..);
    
    对错:

    @GET("bills/")
    Call<YourResponse> getListOfSponsors(....);
    
    @GET(“bills/”)
    调用getListofSposors(..);
    
    确保调用此api。。。。。。。。。是否确认…?我已更新我的问题。请检查一下。您的解析类的响应有问题。。。格式请重新检查..尝试了您的代码,但仍然得到java.lang.IllegalStateException:预期的BEGIN_对象是BEGIN_数组,但在响应中是否确实以[]开头和结尾?您的响应以对象而不是数组开始…这是问题您可以根据您的约定命名类。我不知道存在这种情况,尽管它很有意义。谢谢分享!这些中间对象通常被称为数据转换对象,DTO-因此,如果您需要进一步搜索这些对象,请记住。您好,我也有同样的问题,我如上所述,我确实得到了几个赞助商结果对象作为一个列表,之后如何从中提取SponsorValue对象?我试图这样做,但我只收到null,因此,我做错了什么?有人能给我举一个例子来提取相应对象的字段吗?效果很好
        @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;
        }
    
    }
    
    @GET("bills/")
    Call<List<YourResponse>> getListOfSponsors(....);
    
    @GET("bills/")
    Call<YourResponse> getListOfSponsors(....);