从kotlin中的多个对象内部获取数组

从kotlin中的多个对象内部获取数组,kotlin,Kotlin,我正在尝试建立一个简单的应用程序来提供航班时刻表。问题是,我在json url中有许多对象,这些对象中有数组列表,我无法从对象中获取数组列表,因为我得到了由以下原因引起的错误致命:org.json.JSONException:Value 我的数据json api { "result": { "response": { "airport": { "pluginData": { "schedule": { "arri

我正在尝试建立一个简单的应用程序来提供航班时刻表。问题是,我在json url中有许多对象,这些对象中有数组列表,我无法从对象中获取数组列表,因为我得到了由以下原因引起的错误致命
:org.json.JSONException:Value

我的数据json api

{
  "result": {
    "response": {
      "airport": {
        "pluginData": {
          "schedule": {
            "arrivals": {
              "data": [
                {
                  "flight": {
                    "identification": {
                      "id": null,
                      "row": 4832637003,
                      "number": {
                        "default": "ZP4801",
                        "alternative": null
                      },
                      "callsign": null,
                      "codeshare": null
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
获取数组列表数据的代码

  private fun handleJson (jsonString: String?){

        val jsonArray = JSONArray(jsonString)
        val list =  ArrayList<FlightShdu>()
        var x = 0
        while (x < jsonArray.length()){

            val jsonObject = jsonArray.getJSONObject(x)


            list.add(FlightShdu(

                jsonObject.getInt("id"),
                jsonObject.getString("callsign")

            ))


            x++
        }
        val adapter = ListAdapte(this@MainActivity,list)
        flightShdu_list.adapter = adapter

    }
private-fun-handleJson(jsonString:String?){
val jsonArray=jsonArray(jsonString)
val list=ArrayList()
变量x=0
而(x
我通常会建议通过数据类实现JSON的完整结构,因为这种方法可能需要花费大量时间反复运行。。。下面重点介绍一种方法,该方法通过jsonObjects按名称挖掘JSON,然后获取最后一层“标识”,并填充一个可与结果对象序列化的数据类

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import kotlinx.serialization.json.JsonDecodingException
import kotlinx.serialization.json.JsonElement

val data =
"""
{
 "result": {
  "response": {
   "airport": {
    "pluginData": {
     "schedule": {
      "arrivals": {
       "data": [{
        "flight": {
         "identification": {
          "id": null,
          "row": 4832637003,
          "number": {
           "default": "ZP4801",
           "alternative": null
          },
         "callsign": null,
         "codeshare": null
         }
        }
       }]
      }
     }
    }
   }
  }
 }
}
"""

@Serializable
data class FlightIdentification(
    val id: Int?,
    val row: String,
    val number: IdentificationNumber,
    val callsign: String?,
    val codeshare: String?
) {
    @Serializable
    data class IdentificationNumber(
        val default: String,
        val alternative: String?
    )
}

val json = Json(JsonConfiguration.Stable)

fun JsonElement?.get(name: String): JsonElement? {
    return if (this == null) null
    else this.jsonObject[name]
}

fun handleJson(jsonString: String) {
    val obj = json.parseJson(jsonString)

    val data = obj.get("result").get("response").get("airport").get("pluginData")
        .get("schedule").get("arrivals").get("data")

    if (data != null) {
        val flight = data.jsonArray[0]
            .get("flight").get("identification")
        try {
            val res = json.parse(FlightIdentification.serializer(), flight.toString())

            println(res)
        } catch (e: JsonDecodingException) {
            println("Decode: ${e.message}")
        }
    }
}

handleJson(data)

val-jsonArray=jsonArray(jsonString)
。JSON数组以
[
开头。您的JSON以
{
开头。因此它是一个对象,而不是数组。@JBNizet我想从JSON中获取的对象位于
数据[1]
中,您可以查看我的链接[您的链接无效。请在问题本身中公布jsonString的实际值以及异常的准确完整堆栈跟踪。@JB Nizet l更新了我的有问题的链接。您可以选中将字符串解析为JSONObject。然后您可以访问名为“result”的唯一属性由于它的值再次以
{
开头,因此这也是一个JSONObject。因此,您可以访问其名为“airport”的唯一属性。重复此操作,直到到达名为“data”的属性。它的值以
[
开头,因此这次它是一个数组,而不是一个对象。它的第一个元素在索引0处以
开头{
,所以它又是一个对象。你现在应该明白了。