[Android/Jetpack Compose] Json Serearization ~ How to ignore missing fields ~

Android

What I faced on the problem

When I retrieve the json data from API, I faced an issue.

The error occured when retrieving the data when serearizing because of less parameters. If your model class doesn't cover all parameters you would face problem.
But you have a solution to solve it.

Solution

Make new parser instead of the perser Kotlin provides by default.

    private val jsonParser =
        Json {
            ignoreUnknownKeys = true
        }

    fun parseJsonNonFieldIgnorable(result: String): List<Channel> {
        val apiResponse = jsonParser.decodeFromString<apiResponse>(result)
        return fc2ApiResponse.channel
    }

コメント

タイトルとURLをコピーしました