Home > OS >  How to parse this specific json object to display driver name and lastname on my ui?
How to parse this specific json object to display driver name and lastname on my ui?

Time:09-01

I am trying to parse the following api endpoint.

http://ergast.com/api/f1/current/driverStandings.json

I'm confused about the structure of the json response. specificaly i cant find a way to display driver's name and last name in a recycler view.

i've managed to display data from another json file of the same endpoint, but not from this one.

spent 2-3 days researching and googling.

CodePudding user response:

There is a Plugin in Android Studio called JSON to Kotlin Class. You just have to copy the JSON reponse and paste it there in the plugin and it will generate the necessary classes for you, so you can visualize it more clearly. So what you would end up is:

data class ExampleJson2KtKotlin (
  @SerializedName("MRData" ) var MRData : MRData? = MRData()
)

data class MRData (
  @SerializedName("xmlns"          ) var xmlns          : String?         = null,
  @SerializedName("series"         ) var series         : String?         = null,
  @SerializedName("url"            ) var url            : String?         = null,
  @SerializedName("limit"          ) var limit          : String?         = null,
  @SerializedName("offset"         ) var offset         : String?         = null,
  @SerializedName("total"          ) var total          : String?         = null,
  @SerializedName("StandingsTable" ) var StandingsTable : StandingsTable? = StandingsTable()
)

data class StandingsTable (
  @SerializedName("season"         ) var season         : String?                   = null,
  @SerializedName("StandingsLists" ) var StandingsLists : ArrayList<StandingsLists> = arrayListOf()
)

data class StandingsLists (
  @SerializedName("season"          ) var season          : String?                    = null,
  @SerializedName("round"           ) var round           : String?                    = null,
  @SerializedName("DriverStandings" ) var DriverStandings : ArrayList<DriverStandings> = arrayListOf()
)

data class DriverStandings (
  @SerializedName("position"     ) var position     : String?                 = null,
  @SerializedName("positionText" ) var positionText : String?                 = null,
  @SerializedName("points"       ) var points       : String?                 = null,
  @SerializedName("wins"         ) var wins         : String?                 = null,
  @SerializedName("Driver"       ) var Driver       : Driver?                 = Driver(),
  @SerializedName("Constructors" ) var Constructors : ArrayList<Constructors> = arrayListOf()
)

data class Driver (
  @SerializedName("driverId"        ) var driverId        : String? = null,
  @SerializedName("permanentNumber" ) var permanentNumber : String? = null,
  @SerializedName("code"            ) var code            : String? = null,
  @SerializedName("url"             ) var url             : String? = null,
  @SerializedName("givenName"       ) var givenName       : String? = null,
  @SerializedName("familyName"      ) var familyName      : String? = null,
  @SerializedName("dateOfBirth"     ) var dateOfBirth     : String? = null,
  @SerializedName("nationality"     ) var nationality     : String? = null
)

data class Constructors (
  @SerializedName("constructorId" ) var constructorId : String? = null,
  @SerializedName("url"           ) var url           : String? = null,
  @SerializedName("name"          ) var name          : String? = null,
  @SerializedName("nationality"   ) var nationality   : String? = null
)

And then your response will become like this if you use retrofit

interface RetrofitInterface {
  @GET("example-endpoint")
  suspend fun getDataList(): MRData
}

Then you just collect the data in your ViewModel and display it in the RecycleView.

CodePudding user response:

The basic parsing can be achieved by this way.

 val mrDataJsonObj = response.getJSONObject("MRData")
            val standingTableObj = mrDataJsonObj.getJSONObject("StandingsTable")
            val standingListJsonArray = standingTableObj.getJSONArray("StandingsLists")

            for (i in 0 until standingListJsonArray.length()) {
                val seasonObj = standingListJsonArray.get(i) as JSONObject
                 val driverStandingsArray = seasonObj.getJSONArray("DriverStandings")

                for ( j in 0 until driverStandingsArray.length()){
                        val driverStandingObj = driverStandingsArray.get(j) as JSONObject
                    val driverObj = driverStandingObj.getJSONObject("Driver")

                    // Get All the Driver Attributes from driverObj
                    if(driverObj.has("givenName"))
                    Log.d(TAG, "dummyData: Given Name : "  driverObj.getString("givenName"))

                    if(driverObj.has("familyName"))
                        Log.d(TAG, "dummyData: Family Name "  driverObj.getString("familyName"))

                }

            }
  • Related