Search code examples
androidkotlinandroid-roomtypeconverter

Android Kotlin Room Type Converter for JSON Array


I know there's a lot of other post regarding type converter, but I am facing a different scenario here. What's different is actually the Json format that I am receiving from my server, and I cant ask them to update their format just so I can fix this issue.

I know Im just missing something or just doing it wrong Here's my sample Json format

[{"name":"John","age":30,"cars":["Ford","BMW","Fiat"]}]

The cars format is not object, but JSON Array, and I kinda want to store it under List format in Kotlin

@Entity(tableName = "person")
data class Person(

    @PrimaryKey
    var name: String,

    var age: Int,

    @SerializedName("cars")
    @TypeConverters(ListTypeConverter::class)
    var cars: Array<Car>

)

Any help will be appreciated.

Thanks all


Solution

  • Your POJO it not correct, check below one

       @Entity(tableName = "person")
        data class Person(
    
            @PrimaryKey
            var name: String,
    
            var age: Int,
    
            @SerializedName("cars")
            @TypeConverters(ListTypeConverter::class)
            var cars: Array<String>
        )
    

    cars is a list of String, not Object