【问题标题】:How to Access Child Item In JSON Array如何访问 JSON 数组中的子项
【发布时间】:2016-12-31 00:00:16
【问题描述】:

我正在根据某些搜索词查询 FlickR,响应是 JSON 数组。这是根级别以及前两个结果:

{
 photos: {
   page: 1,
   pages: 4222,
   perpage: 100,
   total: "422175",
      photo: [
          {
          id: "28571356563",
          owner: "8372889@N03",secret: "c4ca6c4364",
          server: "8050",
          farm: 9,
          title: "95040021.jpg",
          ispublic: 1,
          isfriend: 0,
          isfamily: 0,
          url_m: "https://farm9.staticflickr.com/8050/28571356563_c4ca6c4364.jpg",
          height_m: "332",
          width_m: "500"
               },
          {
          id: "28571342883",
          owner: "96125450@N00",
          secret: "db35a59412",
          server: "8307",
          farm: 9,
          title: "Red #Sunset #Silhouette #Trees #Photography",
          ispublic: 1,
          isfriend: 0,
          isfamily: 0,
          url_m: "https://farm9.staticflickr.com/8307/28571342883_db35a59412.jpg",
          height_m: "500",
          width_m: "424"
            },

当我加载结果时,我将遍历所有项目(“总”数字)并加载到 RecyclerView。

最终,我想遍历“照片”,然后获取每张照片的“url_m”。这是我当前通过 Retrofit 对 FlickR API 的调用:

 Call<List<Photo>> call = apiInterface.getImages(mQuery);
            call.enqueue(new Callback<List<Photo>>() {
                @Override
                public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {

                }

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

                }
            });

        }
    });

如何遍历所有照片并获取每张照片的 URL?我为每个模型类设置了完全映射到 FlickR API JSON 对象的模型类:

【问题讨论】:

    标签: java android json retrofit flickr


    【解决方案1】:

    我认为您在代码中实现了错误的 Retrofit 回调。如我所见,您首先收到一个名为 photos 的 JSONObject,其中包含一个 JSONArray photo,因此您的代码应如下所示

    Call<PhotoResult> call = apiInterface.getImages(query);
    call.enqueue(new Callback<PhotoResult>() {...}
    

    如您所见,回调对象是 PhotoResult,这是您的 json 响应的根级别,您应该在其中检索 List&lt;Photo&gt; 集合。

    要生成您的 POJO,您可以使用此网站 http://www.jsonschema2pojo.org/

    你的 POJO 应该是这样的

    public class PhotoResult {
        @SerializedName("photos")
        @Expose
        public Photos photos;
    }
    
    public class Photos {
        @SerializedName("page")
        @Expose
        public Integer page;
        @SerializedName("pages")
        @Expose
        public Integer pages;
        @SerializedName("perpage")
        @Expose
        public Integer perpage;
        @SerializedName("total")
        @Expose
        public String total;
        @SerializedName("photo")
        @Expose
        public List<Photo> photo = new ArrayList<Photo>();
    }
    
    public class Photo {
        ...
    } 
    

    【讨论】:

    • 基于JSON,不就是Call吗?
    • 您的第一个 POJO 应该解析 photos JSONObject,该 POJO 将包含 photos 对象列表
    • 所以基于这个逻辑,如果我想访问“总”项(假设我已经将我的 JSON 映射到 Java),我将在回调代码中输入什么?
    • 在我编辑的答案中,您的 POJO 应该是什么样子的一些示例(如果您愿意,可以添加 getter 和 setter),然后在您的回调中,您将收到 PhotoResult 对象,其中包含 @987654331 @,并且那个包含List&lt;Photo&gt;
    • 要访问total 值,您只需像这样分配response.body().photos.total
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多