首页 > 解决方案 > 在 MVVM 架构中格式化来自改造 API 的嵌套 JSON 响应 - Kotlin

问题描述

我是 kotlin 和 MVVM 的新手,我已经解决这个问题一个星期了,即使在互联网上搜索了一些代码后也没有任何想法。

我正在尝试根据我的需要编辑或修改改造响应(以观察特定类型;说“sf”)并忽略其他不需要的数据。我正在使用可变 livedata 来获取和更新从改造响应到 recylerview 的 JSON 数据。

这是 JSON 数据的链接:http ://www.nactem.ac.uk/software/acromine/dictionary.py?sf=HMM

3 基于 JSON 响应的数据类:

data class sf(

    @SerializedName("sf")
    @Expose
    val sf : String? = null,

    @SerializedName("lfs")
    @Expose
    val lfs : List<lfs>? = null,
)
data class lfs(

    @SerializedName("lf")
    @Expose
    var lf : String? = null,

    @SerializedName("freq")
    @Expose
    var freq : Int? = null,

    @SerializedName("since")
    @Expose
    var since : Int? = null,

    @SerializedName("vars")
    @Expose
    var vars : List<vars>? = null,

) : Serializable
class vars (

    @SerializedName("lf")
    @Expose
    var lf : String? = null,

    @SerializedName("freq")
    @Expose
    var freq : Int? = null,

    @SerializedName("since")
    @Expose
    var since : Int?

): Serializable

活动中的代码:

listUsers = mutableListOf()

        adapter = WordAdapters(this, listUsers )
        recyclerView.adapter = adapter

        wordViewModel = ViewModelProviders.of(this,
            WordViewModelFactory(this)).get(WordsViewModel::class.java)
        wordViewModel!!.getData().observe(this, { t: ArrayList<sf>? ->
            listUsers.clear()
            t?.let { listUsers.addAll(it)
            }
            adapter.notifyDataSetChanged()


  })

视图模型:

class WordsViewModel ( context: Context) : ViewModel() {

private var listData = MutableLiveData<ArrayList<sf>>()

init {
    val wordRepository: WordsRepository by lazy {
            WordsRepository
        }
        //if (context.isInternetAvailable()) {
            listData = wordRepository.getMutableLiveData(context)
       // }
    }

    fun getData(): MutableLiveData<ArrayList<sf>> {
        return listData
    } }

存储库:

    object WordsRepository {
    
        var call: Call<MutableList<sf>>? = null
        fun getMutableLiveData(context: Context) : MutableLiveData<ArrayList<sf>> {
    
            val mutableLiveData = MutableLiveData<ArrayList<sf>>()
    
            //context.showProgressBar()
            call = NetworkApiClient.apiService.getWordsMatching("HMM")
            call!!.enqueue(object : Callback<MutableList<sf>> {
                override fun onFailure(call: Call<MutableList<sf>>, t: Throwable) {
                    //hideProgressBar()
                    Log.e("error", t.localizedMessage.toString())
                }
                override fun onResponse(call: Call<MutableList<sf>>, response: 
                            Response<MutableList<sf>>)
                {
                    //hideProgressBar()
                    if (!response.isSuccessful){
                        Log.e("Code " , response.code().toString());
                        return
                    }
                    val raw: okhttp3.Response = response.raw()
                    val usersResponse : MutableList<sf>? = response.body()
                   /* if (usersResponse != null) {
                        for( movie in usersResponse[0].lfs!!){
                            Log.v("MainActivity", movie.vars.toString())
                        }
                    }*/
                    Log.e("Output : ", usersResponse.toString())
                    usersResponse?.let { mutableLiveData.value = it as ArrayList<sf> }
                }
    
            })
    
            return mutableLiveData
        }
    }

这是 JSON 的基本结构:这里“sf”是一个字符串,lfs 是数组,根据这个 JSON 响应链接,我得到 8 个 lfs 数组,但目前解析后的 recyclecount 是 1,这在适配器 itemcount 中是相同的方法,所以我在 recylerview 中显示一行,其余的被忽略。

JSON响应:

[
{
"sf":"HMM",
"lfs":[
{
"lf":"heavy meromyosin",
"freq":267,
"since":1971,
"vars":[
{
"lf":"heavy meromyosin",
"freq":244,
"since":1971
},
{
"lf":"Heavy meromyosin",
"freq":12,
"since":1975
},
{
"lf":"H-meromyosin",
"freq":5,
"since":1975
},
{
"lf":"heavy-meromyosin",
"freq":4,
"since":1977
},
{
"lf":"heavy meromyosin",
"freq":1,
"since":1976
},
{
"lf":"H-Meromyosin",
"freq":1,
"since":1976
}
]
},

我想在响应后忽略“sf”字符串并解析存在于“lfs”的“sf”下的ArrayList,因此基于“lfs”我需要显示数据。

可变实时数据不接受除 sf 之外的任何其他类型,因为我将观察者放在上面。

标签: androidjsonkotlinmvvmretrofit

解决方案


在您发布的 json 中,只有一个父项(一个 sf ),但您实际上是在尝试传递 8 个 lfs 子项。你必须在某个地方执行这样的转换,它可以直接在网络调用上,像这样:

usersResponse?.let { mutableLiveData.value = it[0].lfs as ArrayList }

考虑两件事:

  1. 在寻找第一项之前检查“它”是否不为空可能会更好。

  2. 这仅适用于您在父数组上始终只有一项的情况(这听起来很奇怪,因为如果是这种情况,那么服务应该返回一个对象,而不是列表,作为 json 的根。如果您将收到更多除了一个对象,您还必须将响应映射到一个 lfs 列表中。类似的东西(伪代码,因为我来自我的手机):

    It.map(项目-> item.lfs)


推荐阅读