So I am currently trying to create an android app capable of displaying calendar bookings. When I retrieve the bookings I get the following JSON:
{
"id": "dqflkgjkzmvjdcdfdzs56f4sd7",
"subject": "Test Meeting",
"start": {
"dateTime": "2021-08-02T14:00:00.0000000",
"timeZone": "UTC"
},
"end": {
"dateTime": "2021-08-02T14:30:00.0000000",
"timeZone": "UTC"
},
"organiser": "Kermit"
}
How do you cast this to a Parcelable class? I started to make the class like this:
@Parcelize
class Booking(
val id: String,
val subject: String,
val start: String, /* This */
val end: String, /* and this has to change to an object*/
val organiser: String,
): Parcelable
Now I still need to find the type for the start and end objects as they have properties themselves and defining it as a separate object seems unnecessary, but that might because I am used to the Angular syntax.
Any ideas on how to cast that JSON data to a Parcelable class?
CodePudding user response:
Usually, you should do with the below way, this would be the better way.
defining it as a separate object seems unnecessary
At least one object is required to handle the Start & end.
@Parcelize
data class Booking(
@SerializedName("id")
var id: String?,
@SerializedName("subject")
var subject: String?,
@SerializedName("start")
var start: TimeData?,
@SerializedName("end")
var end: TimeData?,
@SerializedName("organiser")
var organiser: String?
): Parcelable {
@Parcelize
data class TimeData(
@SerializedName("dateTime")
var dateTime: String?,
@SerializedName("timeZone")
var timeZone: String?
): Parcelable
}
Use JSON to Kotlin Class plugin in you android studio, it will help to generate data class
from the JSON