如何在Kotlin中解析下面的Json数据?

时间:2018-08-27 06:42:41

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

我需要解析此信息-

[
{
    "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库。

2 个答案:

答案 0 :(得分:3)

对于您的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)

答案 1 :(得分:0)

 **You can use below code to parse given json in kotlin**

  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")
            }
        }
    }