如何在 android jetpack 库中使用改造时使用此动态 json 键创建数据 class

how to create data class with this dynamic json keys while using retrofit and andorid jetpack libraries

这是json输出:

{"query":{"apikey":"...","base_currency":"USD","timestamp":1635972203},"data":{"JPY":113.99127,"CNY":6.39464,"CHF":0.9114,"CAD":1.23881,"MXN":20.54423,"INR":74.44808,"BRL":5.57063,"RUB":71.80098,"KRW":1175.11443,"IDR":14295.1734,"TRY":9.63691,"SAR":3.75119,"SEK":8.52554,"NGN":410.22181,"PLN":3.94541,"ARS":99.81213,"NOK":8.49529,"TWD":27.78459,"IRR":42000.64577,"AED":3.67284,"COP":3827.77643,"THB":33.32047,"ZAR":15.23269,"DKK":6.40357,"MYR":4.15212,"SGD":1.34783,"ILS":3.11624,"HKD":7.78416,"EGP":15.7003,"PHP":50.65881,"CLP":811.73282,"PKR":169.4547,"IQD":1458.01958,"DZD":136.722,"KZT":428.93534,"QAR":3.6499,"CZK":21.94293,"PEN":4.0008,"RON":4.25921,"VND":22747.41599,"BDT":85.57148,"HUF":308.78687,"UAH":26.25062,"AOA":598.0065,"MAD":9.06226,"OMR":0.38491,"CUC":24.00026,"BYR":2.00003,"AZN":1.69502,"LKR":200.00259,"SDG":438.90856,"SYP":2511.07513,"MMK":1746.02836,"DOP":56.29093,"UZS":10690.31508,"KES":111.25137,"GTQ":7.73108,"URY":44.18107,"HRV":6.47553,"MOP":8.01811,"ETB":47.31305,"CRC":635.74442,"TZS":2298.03956,"TMT":3.49009,"TND":2.80635,"PAB":1.00002,"LBP":1505.5263,"RSD":101.16202,"LYD":4.54568,"GHS":6.00013,"YER":249.956,"BOB":6.82018,"BHD":0.377,"CDF":1999.22628,"PYG":6875.19435,"UGX":3550.05822,"SVC":8.7497,"TTD":6.74137,"AFN":90.84208,"NPR":119.13277,"HNL":24.06657,"BIH":1.68483,"BND":1.34753,"ISK":129.16264,"KHR":4060.117,"GEL":3.14003,"MZN":63.22108,"BWP":11.45513,"PGK":3.5113,"JMD":153.22216,"XAF":564.86281,"NAD":15.2189,"ALL":105.53113,"SSP":391.0052,"MUR":42.90097,"MNT":2830.04693,"NIO":35.21094,"LAK":10330.27262,"MKD":53.08156,"AMD":474.80501,"MGA":3928.06091,"XPF":102.48118,"TJS":11.26034,"HTG":98.0013,"BSD":1.00003,"MDL":17.41883,"RWF":1018.02194,"KGS":84.77099,"GNF":9510.20822,"SRD":21.40242,"SLL":10779.18736,"XOF":568.81159,"MWK":807.36713,"FJD":2.06806,"ERN":15.05028,"SZL":15.21372,"GYD":207.78611,"BIF":1980.25293,"KYD":0.82002,"MVR":15.42042,"LSL":15.23032,"LRD":146.80405,"CVE":94.95278,"DJF":177.50237,"SCR":14.42749,"SOS":575.00647,"GMD":52.15123,"KMF":424.6543,"STD":21.11031,"XRP":0.83002,"AUD":1.34372,"BGN":1.68394,"BTC":0.0159,"JOD":0.70801,"GBP":0.73402,"ETH":0.00022,"EUR":0.86112,"LTC":0,"NZD":1.40184}}

数据部分包含许多键值对,但它们的数量和名称因base_currency而异。 (例如,如果我用美元发送请求,则没有美元密钥,或者如果我用人民币发送请求,则数据部分没有人民币密钥)

那么我应该创建什么样的数据 class 以便我可以将它与 retrofit 和 gsoncreator 库一起使用。 (如果这很重要,我也在尝试使用和学习 Jetpack android 库)

我使用该数据 classes:(我没有使用 gson 注释,因为我相信我的变量名称是正确的,但我尝试它没有帮助)

data class ResponseFromApi(val data: Data,val query: Query)

data class Query(val apikey: String, val base_currency: String, val timestamp: Int)

data class Data(val hashmapForData: HashMap<String, Double>)(我怀疑有些值是整数,但我不确定)

没用。也许这不是问题所在。我不知道,但至少我需要知道,这些 class 是否正确?这样做的正确方法是什么。

而且我不知道如何从改装对象中获取错误消息,因此我无法识别问题。但这是另一个问题。

Arpit Shukla 的回答是正确的。

ResponseFromApi(val query: Query, val data: Map<String,Double>)

它可以将键值映射反序列化为 Map 结构。