如何在Android中通过改装访问JSON响应中的特定值?

时间:2019-05-14 02:05:09

标签: java android api retrofit

我有一个来自api的JSON响应,我可以从中获得版权信息,该信息位于第一层,这是对服务的响应:

{
    "code": 200,
    "status": "Ok",
    "copyright": "© 2019 MARVEL",
    "attributionText": "Data provided by Marvel. © 2019 MARVEL",
    "attributionHTML": "<a href=\"http://marvel.com\">Data provided by Marvel. © 2019 MARVEL</a>",
    "etag": "a1d8666dc86abda3bd2edf99d09446da82626c4b",
    "data": {
        "offset": 0,
        "limit": 20,
        "total": 1,
        "count": 1,
        "results": [
            {
                "id": 1009664,
                "name": "Thor",
                "description": "As the Norse God of thunder and lightning, Thor wields one of the greatest weapons ever made, the enchanted hammer Mjolnir. While others have described Thor as an over-muscled, oafish imbecile, he's quite smart and compassionate.  He's self-assured, and he would never, ever stop fighting for a worthwhile cause.",
.
.
.
}

我有这样的吸气剂和吸气剂:

private int id;
    private String name;
    private String description;
    private String thumbnail;

    private String copyright;

    public Heroe() {}


    public Heroe(String copyright, int id, String name, String description, String thumbnail) {
        this.copyright = copyright;
        this.id = id;
        this.name = name;
        this.description = description;
        this.thumbnail = thumbnail;
    }

但是我想从数据中获取结果。要获取ID,名称和说明,这是我的代码:

Android response service from MARVEL

2 个答案:

答案 0 :(得分:1)

由于json响应具有子节点,因此请尝试创建自定义响应

HeroResponse.java

class HeroResponse {
    private int code;
    private String status;
    //.. other variables
    @SerializedName("data")
    private Hero data

    //.. Getter and setter
}

Hero.java

class Hero {
    private int offset;
    private int limit;
    //.. other variables
    @SerializedName("results")
    private HeroDetail results

    //.. Getter and setter
}

HeroDetail.java

class HeroDetail {
    private int id;
    private String name;
    private String description

    //.. Getter and setter
}

然后在您的改造电话上

heroeCall.enqueue(
  //..
  public void onResponse(Call<HeroResponse> call, Response<HeroResponse> response)  {
     HeroResponse resp = response.body();
     resp.copyright
     resp.data.offset
     resp.data.results.id
  }
)

如果您要使用注释'com.google.code.gson:gson:2.8.5'

,也不要忘记在应用程序build.gradle中实现SerializedName

希望这会有所帮助。

答案 1 :(得分:0)

首先在模型类中创建getter()和setter()方法

示例:-

public class RetroPhoto {

    @SerializedName("id")
    private Integer id;
    @SerializedName("title")
    private String title;
    @SerializedName("item")
    public Item a;

    public RetroPhoto(Integer albumId, Integer id, String title, String     url, String thumbnailUrl) {
        this.albumId = albumId;
        this.id = id;
        this.title = title;
        this.url = url;
        this.thumbnailUrl = thumbnailUrl;
    }

    public Integer getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Item getItem11() {
        return a;
    }

    public void setItem11(Item item11) {
        this.a = item11;
    }

    public class Item{
       @SerializedName("id_item")
       private String idItem;
       @SerializedName("id_subcategory")
       private String idSubcategory;

       public String getIdItem() {
        return idItem;
       }

       public void setIdItem(String idItem) {
        this.idItem = idItem;
       }

       public String getIdSubcategory() {
        return idSubcategory;
       }

       public void setIdSubcategory(String idSubcategory) {
        this.idSubcategory = idSubcategory;
       }
    }
}

如何获取idSubcategory:-

RetroPhoto.Item ri = response.getItem11();
String idSubcategory = ri.getIdSubcategory();