Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何访问JSON数组中的子项_Java_Android_Json_Retrofit_Flickr - Fatal编程技术网

Java 如何访问JSON数组中的子项

Java 如何访问JSON数组中的子项,java,android,json,retrofit,flickr,Java,Android,Json,Retrofit,Flickr,我根据某些搜索词查询FlickR,响应是JSON数组。下面是根级别以及前两个结果: { photos: { page: 1, pages: 4222, perpage: 100, total: "422175", photo: [ { id: "28571356563", owner: "8372889@N03",secret: "c4ca6c4364", server: "80

我根据某些搜索词查询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”。以下是我当前通过改装对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) {

                }
            });

        }
    });
Call-Call=apinterface.getImages(mQuery);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
});

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

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

Call<PhotoResult> call = apiInterface.getImages(query);
call.enqueue(new Callback<PhotoResult>() {...}
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,不是吗?您的第一个POJO应该解析
photos
JSONObject,该POJO将包含
photos
objectsSo列表,基于该逻辑,如果我想访问“total”项(假设我已经将JSON映射到Java),我将在回调代码中输入什么?在我编辑的答案中,有一些示例说明了POJO的外观(如果愿意,可以添加getter和setter),然后在回调中,您将收到
PhotoResult
对象,其中包含
照片
,其中一个包含
列表
以访问
总计
值,您只需像这样分配
response.body().photos.total