Ibramazin
Ibramazin

Reputation: 1018

Data class primary constructor must have only property (val / var) parameters

I am converting my Json into data object class:

The JSON comes in this format:

entry":[
{
 "im:name": {"label":"Growin' Up"}
 .... 
}

So in my data class I have:

data class Entry(
val im:name: ImName
)

But I am having an arror:

Data class primary constructor must have only property (val / var) parameters

I cant change the JSON returned

Upvotes: 1

Views: 706

Answers (1)

Mohamed Rejeb
Mohamed Rejeb

Reputation: 2639

im:name is an invalid variable name in kotlin, if you are using Moshi library for conversion you can use @Json annoation so your you data class should looks like this:

import com.squareup.moshi.Json

data class Entry(
    @Json(name = "im:name")
    val imName: ImName
)

If you are using Gson library, you can use @SerializedName annotation which is simular to @Json so your data class should looks like this:

import com.google.gson.annotations.SerializedName

data class Entry(
    @SerializedName("im:name")
    val imName: ImName
)

Upvotes: 4

Related Questions