jsonkotlinklaxon

Can't parse json in Kotlin


I have a json like this:

{"ok":true,"result":[{"update_id":853803195,
"message":{"message_id":313,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594723984,"text":"/start","entities":[{"offset":0,"length":6,"type":"bot_command"}]}},{"update_id":853803196,
"message":{"message_id":314,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594723986,"text":"e"}},{"update_id":853803197,
"message":{"message_id":325,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594734252,"text":"\ud83d\ude06"}},{"update_id":853803198,
"message":{"message_id":328,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594736358,"text":"5"}}]}

I'm using klaxon library. Like in the first example I created a class:

class Response(val ok: Boolean, val result: String)

I was trying to save the second parameter to a string, so I could parse it, too, later. But I get a exception like this:

Unable to instantiate Response with parameters [ok: true, result: [, , , ]]

I tried making result a JsonObject (or a JsonArray of JsonObjects) and got this

Unable to instantiate JsonObject with parameters []

The only thing that more or less worked was a List<Any>. Result becomes a list of java objects like this:

[java.lang.Object@680362a, java.lang.Object@3569edd5, java.lang.Object@1f651cd8, java.lang.Object@7d0332e1]

But I don't know how to deal with them.
So my question is what do I do? How do I get the result I will be able to work with?


Solution

  • Try this class

    import com.beust.klaxon.*
    
    private val klaxon = Klaxon()
    
    data class User (
        val ok: Boolean,
        val result: List<Result>
    ) {
        public fun toJson() = klaxon.toJsonString(this)
    
        companion object {
            public fun fromJson(json: String) = klaxon.parse<User>(json)
        }
    }
    
    data class Result (
        @Json(name = "update_id")
        val updateID: Long,
    
        val message: Message
    )
    
    data class Message (
        @Json(name = "message_id")
        val messageID: Long,
    
        val from: From,
        val chat: Chat,
        val date: Long,
        val text: String,
        val entities: List<Entity>? = null
    )
    
    data class Chat (
        val id: Long,
    
        @Json(name = "first_name")
        val firstName: String,
    
        val username: String,
        val type: String
    )
    
    data class Entity (
        val offset: Long,
        val length: Long,
        val type: String
    )
    
    data class From (
        val id: Long,
    
        @Json(name = "is_bot")
        val isBot: Boolean,
    
        @Json(name = "first_name")
        val firstName: String,
    
        val username: String,
    
        @Json(name = "language_code")
        val languageCode: String
    )