如何从android studio项目中的JsonObject提取数据
我正在android studio中开发RESTAPI应用程序。我想从一个JsonObject中提取数据,在使用翻新库检索数据时,我使用模型类保存了这个JsonObject。但它显示了以下错误如何从android studio项目中的JsonObject提取数据,android,json,kotlin,retrofit,Android,Json,Kotlin,Retrofit,我正在android studio中开发RESTAPI应用程序。我想从一个JsonObject中提取数据,在使用翻新库检索数据时,我使用模型类保存了这个JsonObject。但它显示了以下错误 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.covid_19updates, PID: 15236 java.lang.RuntimeException: java.lang.reflect.Invocation
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.covid_19updates, PID: 15236
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: org.json.JSONException: No value for flag
at org.json.JSONObject.get(JSONObject.java:400)
at org.json.JSONObject.getJSONObject(JSONObject.java:620)
at com.example.covid_19updates.MainActivity$onCreate$1.onResponse(MainActivity.kt:52)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$hVGjmafRi6VitDIrPNdoFizVAdk.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
我提取的单个数据对象如下所示:
{"updated":1606563124609,"country":"Afghanistan","countryInfo":{"_id":4,"iso2":"AF","iso3":"AFG","lat":33,"long":65,"flag":"https://disease.sh/assets/img/flags/af.png"},"cases":45966,"todayCases":127,"deaths":1752,"todayDeaths":12,"recovered":36709,"todayRecovered":414,"active":7505,"critical":93,"casesPerOneMillion":1170,"deathsPerOneMillion":45,"tests":146413,"testsPerOneMillion":3727,"population":39280785,"continent":"Asia","oneCasePerPeople":855,"oneDeathPerPeople":22421,"oneTestPerPeople":268,"activePerOneMillion":191.06,"recoveredPerOneMillion":934.53,"criticalPerOneMillion":2.37}
我想检索countryInfo对象中的数据。下面是我的模态类
data class Covid(
var updated: Long,
var country:String,
var countryInfo:JSONObject,
var cases:Int,
var todayCases:Int,
var deaths:Int,
var todayDeaths:Int,
var recovered:Int,
var todayRecovered:Int,
var active:Int,
var critical:Int,
var casesPerOneMillion:Double,
var deathsPerOneMillion:Double,
var test:Long,
var testPerOneMillion:Double,
var population:Long,
var continent:String,
var oneCasePerPeople:Double,
var oneDeathPerPeople:Double,
var oneTestPerPeople:Double,
var activePerOneMillion:Double,
var recoveredPerOneMilion:Double,
var criticalPerOneMillion:Double
)
还有我为获取数据而编写的代码
override fun onResponse(call: Call<List<Covid>>, response: Response<List<Covid>>) {
var list = response.body()
Log.d("Response: ",response.toString())
var countryList = ArrayList<String>()
var countList = ArrayList<String>()
var activeList = ArrayList<String>()
var deathList = ArrayList<String>()
var recoveredList = ArrayList<String>()
var imageList = ArrayList<String>()
list?.map { item ->
countryList.add(item.country.toString())
var totalCases = "Total Cases: "+item.cases.toString()
var activeCases = "Active Cases: "+item.active.toString()
var deaths = "Total Deaths: "+item.deaths.toString()
var recovered = "Total Recovered: "+item.recovered.toString()
var image = item.countryInfo.getJSONObject("flag").toString()
countList.add(totalCases)
activeList.add(activeCases)
deathList.add(deaths)
recoveredList.add(recovered)
}
override-fun-onResponse(调用:调用,响应:响应){
var list=response.body()
Log.d(“Response:,Response.toString())
var countryList=ArrayList()
var countList=ArrayList()
var activeList=ArrayList()
var deathList=ArrayList()
var recoveredList=ArrayList()
var imageList=ArrayList()
列表?.map{item->
countryList.add(item.country.toString())
var totalCases=“Total Cases:+item.Cases.toString()
var activeCases=“Active Cases:+item.Active.toString()
var deathes=“死亡总数:”+item.deathes.toString()
var recovered=“总回收量:”+item.recovered.toString()
var image=item.countryInfo.getJSONObject(“标志”).toString()
countList.add(totalCases)
添加(activeCases)
死亡运动员。添加(死亡)
recoveredList.add(已恢复)
}
尝试为CountryInfo创建一个单独的数据类
新冠病毒
val countryInfo: CountryInfo,
然后
旁注,您可以使用从JSON自动生成kotlin对象
data class CountryInfo(
val _id: Int,
val iso2: String,
val iso3: String,
val lat: Double, //trust me you should use double on latLng values.
val long: Double,
val flag: String
)