首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用flicker获取JSON数组

无法使用flicker获取JSON数组
EN

Stack Overflow用户
提问于 2018-01-13 16:48:10
回答 1查看 64关注 0票数 0

我使用Flicker来显示图像。我从包含数组的API中获取JSON对象。但无法正确解析。当我试图获取JSON数组时,它会给出一个异常:

不能将org.json.JSONObject类型转换为JSONArray

下面是我的代码片段。

代码语言:javascript
复制
JSONObject jsonObject = DataParser.getDataFromWeb();

        try {
            if (jsonObject != null) {
                if (jsonObject.length() > 0) {
                    JSONArray array = null;
                    try {
                        array = jsonObject.getJSONArray("photos");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    int lenArray = array.length();
                    if (lenArray > 0) {
                        for (int jIndex = 0; jIndex < lenArray; jIndex++) {
                            //my codes
                        }
                    }
                }
            } else {

            }
        } catch (JSONException je) {
            Log.i(DataParser.TAG, "" + je.getLocalizedMessage());
        }

我从请求中得到的响应如下:

代码语言:javascript
复制
{
  "photos": {
    "page": 1,
    "pages": 100,
    "perpage": 5,
    "total": 500,
    "photo": [
      {
        "id": "38935250244",
        "owner": "130108065@N08",
        "secret": "092b54d6be",
        "server": "4740",
        "farm": 5,
        "title": "misty.morning.rise.up",
        "ispublic": 1,
        "isfriend": 0,
        "isfamily": 0
      },
      {
        "id": "27869878289",
        "owner": "62440012@N04",
        "secret": "5e9929c4b1",
        "server": "4752",
        "farm": 5,
        "title": "Incoming",
        "ispublic": 1,
        "isfriend": 0,
        "isfamily": 0
      },
      {
        "id": "38942505174",
        "owner": "45571539@N06",
        "secret": "cbf3f74e37",
        "server": "4741",
        "farm": 5,
        "title": "Golden-eye",
        "ispublic": 1,
        "isfriend": 0,
        "isfamily": 0
      },
      {
        "id": "39656469601",
        "owner": "38945681@N07",
        "secret": "b470e84c18",
        "server": "4696",
        "farm": 5,
        "title": "New Year's Sunrise",
        "ispublic": 1,
        "isfriend": 0,
        "isfamily": 0
      },
      {
        "id": "25775783908",
        "owner": "100250163@N03",
        "secret": "a9dfe8ed85",
        "server": "4742",
        "farm": 5,
        "title": "Dissipate",
        "ispublic": 1,
        "isfriend": 0,
        "isfamily": 0
      }
    ]
  },
  "stat": "ok"
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-13 16:51:59

尝尝这个

代码语言:javascript
复制
        JSONObject jsonObject = DataParser.getDataFromWeb();
        try {
            if (jsonObject != null) {
                JSONObject phots= null;
                try {
                    phots = jsonObject.getJSONObject("photos");
                    JSONArray array = null;
                    try {
                        array = phots.getJSONArray("photo");
                        int lenArray = array.length();
                        if (lenArray > 0) {
                            for (int jIndex = 0; jIndex < lenArray; jIndex++) {
                                //my codes
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48242041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档