【问题标题】:How to retrieve the list of all GitHub repositories of a person? parsing json list with no key value如何检索一个人的所有 GitHub 存储库列表?解析没有键值的json列表
【发布时间】:2022-06-30 00:24:17
【问题描述】:

我知道这个问题已经在this post 得到了同样的问题的回答。但是,我的问题是,最受支持的答案的 Json 响应将是一个没有键值的 json 列表。 (你也可以从official github website查看示例json)

我正在使用 Moshi 库来解析 json。但是,我不知道如何解析未设置键值的 Json 列表。(仅 Json 中存在一个列表,该列表没有键值) 这就是它的样子

[       
    {
    "id": 1296269,
    "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
    "...": "...",
    ...
    },   
    {
    "id": 1296255,
    "node_id": "somevalue",
    "...": "...",   
    }
    ...
]

由于提出此问题的目的是获取用户的存储库列表,因此您可以留下任何代码 sn-p 以获取该类型的列表 Json 到 Kotlin 数据类。

提前感谢您的帮助。

【问题讨论】:

    标签: android json kotlin github moshi


    【解决方案1】:

    数组列表试试这个

    inline fun <reified T> Any?.getResponseInArrayList(): ArrayList<T?>? {
    return try {
        val token: TypeToken<ArrayList<T>> = object : TypeToken<ArrayList<T>>() {}
        val json = Gson().toJson(this)
        Gson().fromJson(json, token.type)
    } catch (e: Exception) {
        Log.e("GSON ERROR ", "" + e.message)
        null
    }
    }
    

    这样使用

    val model =  yourJsonString.getResponseInArrayList<YouJsonModel>()
    

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 2020-12-13
      • 2017-05-02
      • 1970-01-01
      • 2019-12-31
      • 2022-11-10
      • 2015-03-10
      • 1970-01-01
      • 2021-04-26
      相关资源
      最近更新 更多