【问题标题】:How to parse below Json data in Kotlin?如何在 Kotlin 中解析下面的 Json 数据?
【发布时间】:2018-08-27 06:42:41
【问题描述】:

我需要解析这些信息-

[
{
    "artist": "12",
    "image": "23"
},
{
    "video_id": "12",
    "video_title": "23"
},
{
    "video_id": "12",
    "video_title": "23"
},
{
    "video_id": "12",
    "video_title": "23"
},
{
    "video_id": "12",
    "video_title": "23"
},
{
    "video_id": "12",
    "video_title": "23"
}]

如您所见,第一个字段不同,如何在 Kotlin 中以不同方式解析下面的信息和第一个字段。

我将第一部分解析为-

response ->

                for (i in 0..(response.length() -1)){

                    /**
                        FIRST SONG
                     **/
                    val song = response.get(0).toString()

                    val listOfSongs = response.toString()

                    val parser = Parser()
                    val stringBuilder = StringBuilder(song)
                    val json: JsonObject = parser.parse(stringBuilder) as JsonObject
                    val firstArtist = json.string("artist")
                    val firstImage = json.string("image")
                    val intent = Intent(activity,ResultPage::class.java)
                    intent.putExtra("firstArtist",firstArtist)
                    intent.putExtra("firstImage",firstImage)

                    startActivity(intent)
                    /**
                        FIRST SONG
                    **/


                }
            }

我也在这里使用 KLAXON 库。

【问题讨论】:

    标签: kotlin android-volley kotlin-android-extensions kotlin-extension rx-kotlin


    【解决方案1】:

    对于您的 json,这应该可以:

    fun parseResponse(response: String) {
    
        var artist = ""
        var image = ""
        val videoList = ArrayList<Video>()
    
        val jsonArray = JSONArray(response)
    
        (0..5).forEach { index ->
            val jsonObject = jsonArray.getJSONObject(index)
            if (jsonObject.has("artist") && jsonObject.has("image")) {
                artist = jsonObject.getString("artist")
                image = jsonObject.getString("image")
            }
            else if (jsonObject.has("video_id") && jsonObject.has("video_title")) {
                val newVideo = Video(jsonObject.getString("video_id"), jsonObject.getString("video_title"))
                videoList.add(newVideo)
            }
        }
    }
    
    class Video(val id: String, val title: String)
    

    但这种方式非常冗长且不必要。我建议使用像 GSONMoshi 这样的对象映射库。

    为此,您的 json 中的视频列表理想情况下应该是这样的:

    [
        {
            "artist": "12",
            "image": "23",
            "videos": [
                {
                    "video_id": "12",
                    "video_title": "23"
                },
                {
                    "video_id": "12",
                    "video_title": "23"
                },
                {
                    "video_id": "12",
                    "video_title": "23"
                },
                {
                    "video_id": "12",
                    "video_title": "23"
                },
                {
                    "video_id": "12",
                    "video_title": "23"
                }
            ]
        }
    ]
    

    使用这个Json,你可以很容易地为这个对象创建一个类,例如

    class Artist(val id: String, val name: String, val image: String, val videos: List<Video>)
    class Video(@field:Json(name = "video_id") val id: String, @field:Json(name = "video_title") val title: String)
    

    然后像这样轻松解析它们:

        Moshi.Builder().build().adapter(Artist::class.java).fromJson(response)
    

    然后访问此信息,例如:

        val artist = Moshi.Builder().build().adapter(Artist::class.java).fromJson(response)
    
        intent.putExtra("firstArtist",artist?.name)
        intent.putExtra("firstImage",artist?.image)
    

    【讨论】:

    • 我在使用密封类 kotlin 时遇到问题,我无法获取值 json
    【解决方案2】:

    您可以使用下面的代码在 kotlin 中解析给定的 json

      private fun parseJson(jsonResponse: String){
            val jsonArray = JSONArray(jsonResponse)
            for (i in 0..jsonArray!!.length() - 1) {
                val jsonObj = jsonArray.optJSONObject(i)
    
                val artist =jsonObj.optString("artist")
                val image =jsonObj.optString("image")
    
                val videosArray = jsonObj.optJSONArray("videos")
                for (i in 0..videosArray!!.length() - 1) {
                    val videoObj = jsonArray.optJSONObject(i)
                    val video_id =videoObj.optString("video_id")
                    val video_title =videoObj.optString("video_title")
                }
            }
        }
    

    【讨论】:

    • 我认为结合类模型更好
    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 2018-07-23
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多