Home > Back-end >  Correct way to parse eterogeneous arrays of data
Correct way to parse eterogeneous arrays of data

Time:09-21

I have this model that I would like to parse from JSON:

class CFInsertedValuesStructure {


    @SerializedName("id")
    val id : Int? = null

    @SerializedName("value")
    val value : List<String> = listOf();


    @SerializedName("field_id")
    val field_id : String? = null
 

}

There is a problem with the parameter "value" because it isn't always an array of String, sometimes it could be just a String type. So when happens I would like to recognise it and create an array of just one String.

CodePudding user response:

depending on what library you use the json parsing it may require a custom parsing type e.g. for kotlinx.serialization you might need to do something like a custom serializer

https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#specifying-serializer-on-a-property

better still : tell you server-side developer it should always be an array!

  • Related