本文介绍了JSONException:没有照片的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flickr API来使我的应用根据用户搜索显示图像.我不断收到此错误:JSONException: No value for photo

I'm using the Flickr API to get my app to display images depending on user search. I keep getting this error: JSONException: No value for photo

获取照片的电话:

public ArrayList<Category> processResults(Response response) {
    ArrayList<Category> categories = new ArrayList<>();

    try {
        String jsonData = response.body().string();
        if (response.isSuccessful()) {
            JSONObject flickrJSON = new JSONObject(jsonData);
            //json data
            JSONArray photoJSON = flickrJSON.getJSONArray("photo");
        }
    }
}

json格式是这样:

the json format is this:

{
    photos: { page: 1,
              pages: 2165,
              perpage: 100,
              total: "216413",
              photo: [ { id: "37095719122",
              ....
    }
}

推荐答案

由于代码的new JSONObject()部分工作正常,因此可以安全地假设您获取的JSON对象是有效的,在这种情况下,您的实际JSON对象是有效的必须看起来像这样:

Since the new JSONObject() part of your code works fine, its safe to assume that the JSON object you are getting is valid and in that case your actuall JSON object must look something like this :

{
    photos: { page: 1,
              pages: 2165,
              perpage: 100,
              total: "216413",
              photo: [ { id: "37095719122",
              .....
    }
}

变量flickerJSON将包含整个对象,并且它仅有的字段是photos,而您尝试访问的photo字段是photos对象的内部字段.

The variable flickerJSON would contain this whole object and the only field it has is photos, while the photo field you are trying to access is an inner field of photos object.

因此,您可以像这样访问photo字段:

Hence, you can access the photo field like this :

JSONArray photoJSON = flickrJSON.getJSONObject("photos").getJSONArray("photo");

这篇关于JSONException:没有照片的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:09