Thread
#serialization
    Spikey Sanju

    Spikey Sanju

    1 year ago
    Hello all, I’m getting error when I try deserialize an object from JSON using Kotlin Serialization. I get the following error 👇
    Here’s my JSON file 👇
    [
      {
        "emoji": "😍",
        "description": "Love",
        "category": "this is category"
      },
      {
        "emoji": "🚀",
        "description": "Rocket",
        "category": "this is category"
      },
      {
        "emoji": "✅",
        "description": "Check",
        "category": "this is category"
      },
      {
        "emoji": "🛍",
        "description": "Shopping",
        "category": "this is category"
      },
      {
        "emoji": "🔥",
        "description": "Fire",
        "category": "this is category"
      }
    ]
    Here’s the simple code snippet 👇
    import kotlinx.serialization.decodeFromString
    import kotlinx.serialization.json.*
    import model.AllEmojiItem
    import java.io.File
    import java.util.*
    
    fun main() {
        val scanner = Scanner(System.`in`)
        println("Enter the file path: ")
        val filePath = scanner.nextLine()
        println("File is $filePath")
        val emojiJson = File(filePath).readText()
    
        // reading filePath 👀
        println("Reading filePath 👀... \n")
    
        // decode from string
        val decodeEmoji = Json.decodeFromString<AllEmojiItem>(emojiJson)
        println("Decoding :$decodeEmoji")
    }
    Here’s model class 👇
    package model
    
    import kotlinx.serialization.Serializable
    
    @Serializable
    data class AllEmojiItem(
        val aliases: List<String>,
        val category: String,
        val description: String,
        val emoji: String,
        val ios_version: String,
        val skin_tones: Boolean,
        val tags: List<String>,
        val unicode_version: String
    )
    Javier

    Javier

    1 year ago
    you have a list of objects, not an object
    v

    Vampire

    1 year ago
    But it should then complain that there is
    [
    instead of
    {
    , not
    EOF
    , shouldn't it?