1

l am try to build simple app provide flight schedule . the problem is l have many object in json url and the array list inside of these object and l cant to get array list from objects because l got error fatal Caused by: org.json.JSONException: Value

my data json api

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

my code for getting data json of array list

  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

    }
12
  • 1
    val jsonArray = JSONArray(jsonString). JSON arrays start with [. Your JSON start with {. So it's an object, not an array. Commented Nov 4, 2018 at 14:07
  • @JBNizet the objects i want get from json is inside of data [1] you can take look in my link [jsoneditoronline.org/?id=38a83e0996cd4d1a823a55755907c629] Commented Nov 4, 2018 at 14:11
  • Your link is invalid. Post, in the question itself, the actual value of jsonString, and the exact and complete stack trace of the exception. Commented Nov 4, 2018 at 14:14
  • @JB Nizet l updated my link in question you can check Commented Nov 4, 2018 at 14:16
  • 1
    Parse your string as a JSONObject. You can then access the unique property, named "result", of that JSONObject. Since its' value, once again, starts with {, this is a JSONObject, too. So you can access its unique property named ""airport". Repeat until you reach the property named "data". Its value starts with a [, so it's an array this time, not an object. Its first element, at index 0, starts with a {, so it's once again an object. You should now get the idea. Commented Nov 4, 2018 at 14:27

1 Answer 1

1

I would normally suggest for a full structure of the JSON via data classes, as this methodology could potentially be expensive to run multiple times over and over... The following highlights a method to dig into the JSON through jsonObjects by name, and then take the final layer of "identification" and populates a data class that is serializable with the resulting object

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.