gpt4 book ai didi

android - 将 kotlin 数据类转换为 json 字符串

转载 作者:行者123 更新时间:2023-12-02 16:41:32 24 4
gpt4 key购买 nike

我有一个具有此类定义的数据类

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
)

我需要将此类转换为这样的 json 字符串

{"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":"sameh88@ensofia.com","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"}]}

最佳答案

我可以看到您想要序列化 ​​ArrayList<UserPatient> 。您可以通过 Gson 轻松完成.

示例:

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

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

输出:

{"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":"sameh88@ensofia.com","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"}]}

关于android - 将 kotlin 数据类转换为 json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56961685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com