I am working on a requirement, where as when making a service call to the API, if it is success, the response object will be a set of key-value in the following format:
string : {string: string}
For instance, the json response will be something similar like this:
{
"contentName": "mobile",
"language": "nz-en",
"content": {
"data_pack_description_0": {"text": "Data only"},
"data_pack25_description_0": {"text": "Pack 25"}
"buying_title_0": {"text": "Buy an item"}
"buying_description_0": {"text": "This is an item for sale"}
}
}
I am planning to use Gson Serialize in the data model class, something similar like:
data class ResponseObject(
@SerializedName("contentName")
val contentName: String
@SerializedName("language")
val language: String
@SerializedName("content")
val content: Content
)
data class Content(
//What should it be here so that we can use @SerializedName???
)
However, the problem is that, the key in the json string is dynamic, means that we don't know exactly what it is. That's why @SerializedName
doesn't work cause we don't have the name.
How can I serialise the above json string into a Model so that it can be used in the ViewModel layer (the app I am working on is MVVM Kotlin)?
In fact, the question will be what's the best way to serialise information inside "content", knowing that we will not know exactly what will be return but we know the format:
"string": {"string" : "string"}
Thanks.