无法解析带有html标签的json响应

时间:2019-07-18 12:25:57

标签: android json kotlin

我正在尝试解析具有HTML标签的json响应并获取无效的json异常,我尝试了此Android JSON parsing with HTML tags,但不幸的是没有成功,现在我正在使用 Jsoup.parse() { {3}},但仍会出现异常

这是我的json响应:  reference

这是我的Kotlin代码,可以使用Jsoup解析json

         if (response != null) {

                      var jsonObject: JSONObject? = null
                      var dataStr = Jsoup.parse(response.string()).text()
                      if (!TextUtils.isEmpty(dataStr)) {

                          jsonObject = JSONObject(dataStr)
                          var d = jsonObject.getJSONObject("d")
                          var result = d.getJSONObject("result")
                          var dataArr = result.getJSONArray("InformativeContent")

                                     for (position in 0 until dataArr.length()) {
                                         var dataObj: JSONObject = dataArr.getJSONObject(position)
                                         var infoPageObj: JSONObject = dataObj.getJSONObject("InfoPage")

                                         var infoPage = InfoPage(
                                                 infoPageObj.getString("Header"),
                                                 infoPageObj.getString("Heading"),
                                                 infoPageObj.getString("IsHTML"),
                                                 infoPageObj.getString("Message"))
                                         introContentList!!.add(infoPage)
                                     }

                                     /**
                                      * update viewPager UI
                                      */
                                     settingUpPager()
                               }
                         }

请让我知道这个问题

2 个答案:

答案 0 :(得分:1)

var infoPageObj: JSONObject = dataObj.getJSONObject("InfoPage") I think this is wrong. according to your image `InfoPage` is a jsonArray.

答案 1 :(得分:0)

您以错误的顺序获取价值。

在json响应“ InfoPage”中返回JSONArray,该数组由类型为InfoPage(“ Header”,“ Heading”,“ isHTML”,“ Message”)的JSON Object组成。

   for (position in 0 until dataArr.length()) {
                                         var dataObj: JSONObject = dataArr.getJSONObject(position)
                                         var infoPageObj: JSONArray = dataObj.getJSONObject("InfoPage")

for (infoPageposition in 0 until infoPageObj.length()) {

          var infoPageJSONObj: JSONObject = dataArr.getJSONObject(infoPageposition)
                                         var infoPage = InfoPage(
                                                 infoPageJSONObj.getString("Header"),
                                                 infoPageJSONObj.getString("Heading"),
                                                 infoPageJSONObj.getString("IsHTML"),
                                                 infoPageJSONObj.getString("Message"))
                                         introContentList!!.add(infoPage)
                                     }
}