Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert kotlin data class into json string

I have a data class with this class definition

data class AccountInfoResponse(
    @SerializedName("userprofile") val userprofiles: ArrayList<UserProfile>,
    @SerializedName("userpatients") val userpatients: ArrayList<UserPatient>
)

class UserPatient (
    @SerializedName("sex") val sex: String,
    @SerializedName("date of birth") val date_of_birth: String,
    @SerializedName("address") val address: String,
    @SerializedName("patientID") val patientId: Int,
    @SerializedName("first name") val first_name: String,
    @SerializedName("clinicName") val clinic_name: String,
    @SerializedName("clinicID") val clinicId: Int,
    @SerializedName("mobile") val mobile: String,
    @SerializedName("last name") val last_name: String
)

I need to convert this class to be json string like this

{"userpatients":[{"sex":"male","date of birth":"2010-01-03","image":"","clinics":[1],"primary_provider":[{"clinic":1,"patient":1,"providers":1}],"role":"patient","last name":"John","address":"300 east main st.  San Jose, Ca 95014","first name":"John","username":"John","email":"[email protected]","mobile":"+88083918427"}],"userpatients":[{"sex":"female","date of birth":"2000-01-01","address":"fawal st1","patientID":1,"first name":"john","clinicName":"light house peds","clinicID":1,"mobile":"8056688042","last name":"john"}]}
like image 973
Amira Elsayed Ismail Avatar asked Jul 09 '19 23:07

Amira Elsayed Ismail


People also ask

What is @SerializedName in Kotlin?

The @SerializedName annotation can be used to serialize a field with a different name instead of an actual field name. We can provide the expected serialized name as an annotation attribute, Gson can make sure to read or write a field with the provided name.

How do you create a data class from JSON in Kotlin?

Plugin generates Kotlin data classes from JSON text. It can find inner classes in nested JSON. You can disable undesirable fields in class, change field name, set it's type to optional, specify default value and add annotations for popular json libraries.


1 Answers

I can see that you want to serialize the ArrayList<UserPatient>. You can do it easily with Gson.

Example:

val response = AccountInfoResponse(/* Here goes the objects that is needed to create instance of this class */)

val jsonString = Gson().toJson(response.userpatients)

Output:

{"userpatients":[{"sex":"male","date of birth":"2010-01-03","image":"","clinics":[1],"primary_provider":[{"clinic":1,"patient":1,"providers":1}],"role":"patient","last name":"John","address":"300 east main st.  San Jose, Ca 95014","first name":"John","username":"John","email":"[email protected]","mobile":"+88083918427"}],"userpatients":[{"sex":"female","date of birth":"2000-01-01","address":"fawal st1","patientID":1,"first name":"john","clinicName":"light house peds","clinicID":1,"mobile":"8056688042","last name":"john"}]}
like image 137
Birju Vachhani Avatar answered Oct 23 '22 09:10

Birju Vachhani