【问题标题】:How to fetch multilevel JSON using volley?如何使用 volley 获取多级 JSON?
【发布时间】:2020-12-05 13:19:17
【问题描述】:

我愿意从每个“显示”数组中获取数据的 JSON 示例

 {
            "score":17.873907,
            "show":{
                "id":139,
                "url":"http://www.tvmaze.com/shows/139/girls",
                "name":"Girls",
                "type":"Scripted",
                "language":"English",
                "genres":[
                    "Drama",
                    "Romance"
                ],
                "status":"Ended",
                "runtime":30,
                "premiered":"2012-04-15",
                "officialSite":"http://www.hbo.com/girls",
                "schedule":{
                    "time":"22:00",
                    "days":[
                        "Sunday"
                    ]
                },
                "rating":{
                    "average":6.7
                },
                "weight":81,
                "network":{
                    "id":8,
                    "name":"HBO",
                    "country":{
                        "name":"United States",
                        "code":"US",
                        "timezone":"America/New_York"
                    }
                },
                "webChannel":null,
                "externals":{
                    "tvrage":30124,
                    "thetvdb":220411,
                    "imdb":"tt1723816"
                },
                "image":{
                    "medium":"http://static.tvmaze.com/uploads/images/medium_portrait/31/78286.jpg",
                    "original":"http://static.tvmaze.com/uploads/images/original_untouched/31/78286.jpg"
                },
                "summary":"<p>This Emmy winning series is a comic look at the assorted humiliations and rare triumphs of a group of girls in their 20s.</p>",
                "updated":1600633829,
                "_links":{
                    "self":{
                        "href":"http://api.tvmaze.com/shows/139"
                    },
                    "previousepisode":{
                        "href":"http://api.tvmaze.com/episodes/1079686"
                    }
                }
            }
        }

我正在使用的请求代码很好地给了我响应,但我无法解析对 JSONObject 的响应

错误显示

org.json.JSONException:不能显示 java.lang.String 类型的值 转换为 JSONObject

【问题讨论】:

  • 什么是新的 JSONObject("show")??
  • 它应该类似于 new JSONObject(response)
  • JSONObject(response) 也显示异常类型无法转换为 JSON 对象。
  • 您可以在将响应转换为 jsonobj 之前记录响应吗?确保你得到一个 json 字符串
  • 请提供您的代码作为文本,而不是图片

标签: android json android-volley


【解决方案1】:

当您传递 JSONObject("show") 时,它正在尝试将 'show' 字符串转换为 json 显然它不是 json Stirng 所以它会抛出错误; 它应该是这样的:

JSONObject obj = new JSONObject(response);
JSONObject obj_show= obj.getJSONObject("show"));
Log.d("show",obj_show.getJSONObject("show").toString());

【讨论】:

  • 仍然给出值异常
【解决方案2】:

解决方案是“我使用嵌套模型类来加载数据,效果很好!”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多