Home > Net >  How can I create my data class for my json file?
How can I create my data class for my json file?

Time:12-03

I am a new kotlin programmer and I cant create data class for json file. I always run into a problem. I've worked with simple json files before, but this confused me a bit. How can I convert this json file to data class. By the way, I tried the json to data class, which is an extension of kotlin, then it outputs a data class like this, frankly, this is not a very readable data class, does anyone have a better idea?

my json file

{
 "result":"success",
 "documentation":"https://www.exchangerate-api.com/docs",
 "terms_of_use":"https://www.exchangerate-api.com/terms",
 "time_last_update_unix":1669939202,
 "time_last_update_utc":"Fri, 02 Dec 2022 00:00:02  0000",
 "time_next_update_unix":1670025602,
 "time_next_update_utc":"Sat, 03 Dec 2022 00:00:02  0000",
 "base_code":"USD",
 "conversion_rates":{
  "USD":1,
  "AED":3.6725,
  "AFN":88.0236,
  "ALL":112.3100,
  "AMD":395.3092,
  "ANG":1.7900,
  "AOA":509.4547,
  "ARS":166.9052,
  "AUD":1.4696,
  "AWG":1.7900,
  "AZN":1.6980,
  "BAM":1.8638,
  "BBD":2.0000,
  "BDT":101.4835,
  "BGN":1.8647,
  "BHD":0.3760,
  "BIF":2053.3857,
  "BMD":1.0000,
  "BND":1.3561,
  "BOB":6.9195,
  "BRL":5.1984,
  "BSD":1.0000,
  "BTN":81.0572,
  "BWP":13.0619,
  "BYN":2.6482,
  "BZD":2.0000,
  "CAD":1.3433,
  "CDF":2054.6915,
  "CHF":0.9394,
  "CLP":890.3480,
  "CNY":7.0548,
  "COP":4815.7969,
  "CRC":601.6168,
  "CUP":24.0000,
  "CVE":105.0763,
  "CZK":23.2743,
  "DJF":177.7210,
  "DKK":7.1093,
  "DOP":54.3860,
  "DZD":138.5331,
  "EGP":24.5835,
  "ERN":15.0000,
  "ETB":53.4963,
  "EUR":0.9530,
  "FJD":2.1960,
  "FKP":0.8183,
  "FOK":7.1093,
  "GBP":0.8183,
  "GEL":2.7103,
  "GGP":0.8183,
  "GHS":14.1003,
  "GIP":0.8183,
  "GMD":63.3901,
  "GNF":8622.0204,
  "GTQ":7.8232,
  "GYD":209.1401,
  "HKD":7.7836,
  "HNL":24.6064,
  "HRK":7.1800,
  "HTG":140.8970,
  "HUF":392.6534,
  "IDR":15442.8385,
  "ILS":3.4027,
  "IMP":0.8183,
  "INR":81.0584,
  "IQD":1458.8962,
  "IRR":42036.3612,
  "ISK":142.4758,
  "JEP":0.8183,
  "JMD":153.8702,
  "JOD":0.7090,
  "JPY":135.7224,
  "KES":123.1351,
  "KGS":84.4410,
  "KHR":4125.2930,
  "KID":1.4694,
  "KMF":468.8174,
  "KRW":1303.6280,
  "KWD":0.2996,
  "KYD":0.8333,
  "KZT":468.1697,
  "LAK":17248.0199,
  "LBP":1507.5000,
  "LKR":363.4521,
  "LRD":153.9744,
  "LSL":17.5733,
  "LYD":4.8894,
  "MAD":10.6209,
  "MDL":19.4574,
  "MGA":4349.9903,
  "MKD":59.4362,
  "MMK":2552.4090,
  "MNT":3422.5815,
  "MOP":8.0171,
  "MRU":38.0126,
  "MUR":43.4017,
  "MVR":15.4048,
  "MWK":1032.9387,
  "MXN":19.1778,
  "MYR":4.4020,
  "MZN":63.9513,
  "NAD":17.5733,
  "NGN":443.8839,
  "NIO":36.3799,
  "NOK":9.7593,
  "NPR":129.6915,
  "NZD":1.5722,
  "OMR":0.3845,
  "PAB":1.0000,
  "PEN":3.8347,
  "PGK":3.5199,
  "PHP":56.1010,
  "PKR":224.1295,
  "PLN":4.4704,
  "PYG":7217.3743,
  "QAR":3.6400,
  "RON":4.6970,
  "RSD":112.1709,
  "RUB":61.1764,
  "RWF":1114.9049,
  "SAR":3.7500,
  "SBD":8.1036,
  "SCR":13.0656,
  "SDG":568.0316,
  "SEK":10.3601,
  "SGD":1.3561,
  "SHP":0.8183,
  "SLE":18.6210,
  "SLL":18620.9957,
  "SOS":568.2695,
  "SRD":30.9539,
  "SSP":644.4877,
  "STN":23.3471,
  "SYP":2504.9740,
  "SZL":17.5733,
  "THB":34.8989,
  "TJS":10.1357,
  "TMT":3.4988,
  "TND":2.9893,
  "TOP":2.3706,
  "TRY":18.6279,
  "TTD":6.7555,
  "TVD":1.4694,
  "TWD":30.4630,
  "TZS":2334.9042,
  "UAH":36.4641,
  "UGX":3745.5147,
  "UYU":39.4017,
  "UZS":11238.8195,
  "VES":11.2548,
  "VND":24496.1101,
  "VUV":120.8927,
  "WST":2.7081,
  "XAF":625.0899,
  "XCD":2.7000,
  "XDR":0.7578,
  "XOF":625.0899,
  "XPF":113.7166,
  "YER":250.1010,
  "ZAR":17.5753,
  "ZMW":17.0952,
  "ZWL":654.0107
 }
}

json to data class kotlin plugin

@Serializable
data class ExchangeDto(
    val base_code: String,
    val conversion_rates: ConversionRates,
    val documentation: String,
    val result: String,
    val terms_of_use: String,
    val time_last_update_unix: Int,
    val time_last_update_utc: String,
    val time_next_update_unix: Int,
    val time_next_update_utc: String
) 

@Serializable
data class ConversionRates(
    val AED: Double,
    val AFN: Double,
    val ALL: Double,
    val AMD: Double,
    val ANG: Double,
    val AOA: Double,
    val ARS: Double,
    val AUD: Double,
    val AWG: Double,
    val AZN: Double,
    val BAM: Double,
    val BBD: Double,
    val BDT: Double,
    val BGN: Double,
    val BHD: Double,
    val BIF: Double,
    val BMD: Double,
    val BND: Double,
    val BOB: Double,
    val BRL: Double,
    val BSD: Double,
    val BTN: Double,
    val BWP: Double,
    val BYN: Double,
    val BZD: Double,
    val CAD: Double,
    val CDF: Double,
    val CHF: Double,
    val CLP: Double,
    val CNY: Double,
    val COP: Double,
    val CRC: Double,
    val CUP: Double,
    val CVE: Double,
    val CZK: Double,
    val DJF: Double,
    val DKK: Double,
    val DOP: Double,
    val DZD: Double,
    val EGP: Double,
    val ERN: Double,
    val ETB: Double,
    val EUR: Double,
    val FJD: Double,
    val FKP: Double,
    val FOK: Double,
    val GBP: Double,
    val GEL: Double,
    val GGP: Double,
    val GHS: Double,
    val GIP: Double,
    val GMD: Double,
    val GNF: Double,
    val GTQ: Double,
    val GYD: Double,
    val HKD: Double,
    val HNL: Double,
    val HRK: Double,
    val HTG: Double,
    val HUF: Double,
    val IDR: Double,
    val ILS: Double,
    val IMP: Double,
    val INR: Double,
    val IQD: Double,
    val IRR: Double,
    val ISK: Double,
    val JEP: Double,
    val JMD: Double,
    val JOD: Double,
    val JPY: Double,
    val KES: Double,
    val KGS: Double,
    val KHR: Double,
    val KID: Double,
    val KMF: Double,
    val KRW: Double,
    val KWD: Double,
    val KYD: Double,
    val KZT: Double,
    val LAK: Double,
    val LBP: Double,
    val LKR: Double,
    val LRD: Double,
    val LSL: Double,
    val LYD: Double,
    val MAD: Double,
    val MDL: Double,
    val MGA: Double,
    val MKD: Double,
    val MMK: Double,
    val MNT: Double,
    val MOP: Double,
    val MRU: Double,
    val MUR: Double,
    val MVR: Double,
    val MWK: Double,
    val MXN: Double,
    val MYR: Double,
    val MZN: Double,
    val NAD: Double,
    val NGN: Double,
    val NIO: Double,
    val NOK: Double,
    val NPR: Double,
    val NZD: Double,
    val OMR: Double,
    val PAB: Double,
    val PEN: Double,
    val PGK: Double,
    val PHP: Double,
    val PKR: Double,
    val PLN: Double,
    val PYG: Double,
    val QAR: Double,
    val RON: Double,
    val RSD: Double,
    val RUB: Double,
    val RWF: Double,
    val SAR: Double,
    val SBD: Double,
    val SCR: Double,
    val SDG: Double,
    val SEK: Double,
    val SGD: Double,
    val SHP: Double,
    val SLE: Double,
    val SLL: Double,
    val SOS: Double,
    val SRD: Double,
    val SSP: Double,
    val STN: Double,
    val SYP: Double,
    val SZL: Double,
    val THB: Double,
    val TJS: Double,
    val TMT: Double,
    val TND: Double,
    val TOP: Double,
    val TRY: Double,
    val TTD: Double,
    val TVD: Double,
    val TWD: Double,
    val TZS: Double,
    val UAH: Double,
    val UGX: Double,
    val USD: Int,
    val UYU: Double,
    val UZS: Double,
    val VES: Double,
    val VND: Double,
    val VUV: Double,
    val WST: Double,
    val XAF: Double,
    val XCD: Double,
    val XDR: Double,
    val XOF: Double,
    val XPF: Double,
    val YER: Double,
    val ZAR: Double,
    val ZMW: Double,
    val ZWL: Double
)

I think it's a bad translation when it's like this. How can I improve this or how can I create my own data class

CodePudding user response:

You can do it like this. Instead of having each field for each currencies, you can make coversion_rates as Map , so currency will be the key and value will the currency value. So it will be easier for iterating all currencies, getting value from currency name and so on...

@Serializable
data class ExchangeDto(
    val base_code: String,

    @TypeConverters(MapTypeConverter::class) //Add annotation
    val conversion_rates: Map<String,Double>,

    val documentation: String,
    val result: String,
    val terms_of_use: String,
    val time_last_update_unix: Int,
    val time_last_update_utc: String,
    val time_next_update_unix: Int,
    val time_next_update_utc: String
) 

Edit:

Type converter for currency map:

object MapTypeConverter {

@TypeConverter
@JvmStatic
fun covertStringToMap(value: String): Map<String, String> {
    return Gson().fromJson(value,  object : TypeToken<Map<String, String>>() {}.type)
}

@TypeConverter
@JvmStatic
fun mapToString(value: Map<String, String>?): String {
    return if(value == null) "" else Gson().toJson(value)
}

}

  • Related