繁体   English   中英

我如何解析这种具有不同名称的 json,或者告诉我如何在回收站视图中显示此响应。 :(

[英]How do I parse this kinda json which has different names , or tel me how do I even display this response in recycler view. :(

{ 
   "code":200,
   "response":{ 
      "Categories":{ 
         "12345":{ 
            "name":"Category1",
            "image":"image1URL"
         },
         "23456":{ 
            "name":"Category2",
            "image":"image2URL"
         },
         "34567":{ 
            "name":"Category13",
            "image":"image3URL"
         },
         "45678":{ 
            "name":"Category14",
            "image":"image4URL"
         },
         "56789":{ 
            "name":"Category15",
            "image":"image5URL"
         }
      }
   }
}

就像它里面有所有的 uid 一样。 我不知道,我需要真正快速的帮助,最后一件事,我不能要求他们修改。

您可以使用JSONObject (org.json 库)解析 object 并像这样遍历videoCategories的子元素的键:

val videoCategories = JSONObject(jsonString).getJSONObject("response").getJSONObject("videoCategories")
videoCategories.keys().forEach { uuid ->
    Log.v("JSONParsing", "uuid: $uuid")
}

或者,当然,使用其他收集操作(如map )将特定元素转换为 model 对象,您可以在回收器视图适配器中使用它:

val videoCategories = JSONObject(jsonString).getJSONObject("response").getJSONObject("videoCategories")
val categories = videoCategories.keys().map { uuid ->
    val categoryObject = videoCateories.getJSONObject(uuid)
    val name = categoryObject.getString("name")
    val image = categoryObject.getString("image")
    Category(uuid, name, image)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM