You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解析键为动态UID的JSON并将响应内容展示在RecyclerView中

动态键JSON解析及RecyclerView展示方案

1. 解析动态键JSON

核心思路:JSON中Categories对应的是键值对结构,不需要提前知道动态UID键名,直接将该字段解析为Map<String, 自定义分类实体>即可,后续提取Map的value集合就能得到分类列表,要是需要用到UID也可以同时提取Map的key。

方案1:Gson解析(最常用,推荐)

先定义对应的实体类:

// 单个分类的实体类
data class CategoryItem(
    val name: String,
    val image: String
)

// 若需要保留动态UID,可扩展实体类
data class CategoryWithUid(
    val uid: String,
    val name: String,
    val image: String
)

// 响应内层结构
data class ResponseInner(
    val Categories: Map<String, CategoryItem>
)

// 最外层响应结构
data class ApiResponse(
    val code: Int,
    val response: ResponseInner
)

解析代码:

val gson = Gson()
val apiResponse = gson.fromJson(yourJsonString, ApiResponse::class.java)
// 不需要UID的场景,直接转列表
val categoryList = apiResponse.response.Categories.values.toList()

// 需要保留UID的场景,转换为带UID的实体列表
val categoryWithUidList = apiResponse.response.Categories.map { entry ->
    CategoryWithUid(
        uid = entry.key,
        name = entry.value.name,
        image = entry.value.image
    )
}

方案2:原生JSONObject解析(无需引入第三方库)

val categoryList = mutableListOf<CategoryItem>()
val rootJson = JSONObject(yourJsonString)
val responseJson = rootJson.getJSONObject("response")
val categoriesJson = responseJson.getJSONObject("Categories")
// 遍历所有动态UID键
val keyIterator = categoriesJson.keys()
while (keyIterator.hasNext()) {
    val uid = keyIterator.next()
    val itemJson = categoriesJson.getJSONObject(uid)
    val item = CategoryItem(
        name = itemJson.getString("name"),
        image = itemJson.getString("image")
    )
    categoryList.add(item)
}

2. 展示到RecyclerView

步骤1:定义列表条目布局item_category.xml

按需放置展示分类名称的TextView和展示分类图的ImageView即可。

步骤2:实现RecyclerView.Adapter

class CategoryAdapter(private val list: List<CategoryItem>) : RecyclerView.Adapter<CategoryAdapter.VH>() {

    inner class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivImg: ImageView = itemView.findViewById(R.id.iv_category_img)
        val tvName: TextView = itemView.findViewById(R.id.tv_category_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_category, parent, false)
        return VH(view)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        val item = list[position]
        holder.tvName.text = item.name
        // 图片加载可根据自己的技术栈选择Glide、Coil等框架,示例如下
        Glide.with(holder.itemView.context)
            .load(item.image)
            .into(holder.ivImg)
    }

    override fun getItemCount() = list.size
}

步骤3:绑定Adapter到RecyclerView

// 解析得到分类列表后绑定即可
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = CategoryAdapter(categoryList)

内容的提问来源于stack exchange,提问作者Nitin Tej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 13:42:03