gson 如何解析这个特定的json对象以在我的ui上显示驱动程序名称和姓氏?

sqougxex  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(147)

我正在尝试解析以下API终结点。
http://ergast.com/api/f1/current/driverStandings.json
我对json响应的结构感到困惑。具体来说,我找不到一种方法来在回收器视图中显示驱动程序的名称和姓氏。
我已经设法显示了来自同一端点的另一个json文件的数据,但不是来自这个端点。
花了2-3天的时间搜索和搜索。

lvjbypge

lvjbypge1#

Android Studio中有一个名为JSON to Kotlin Class的插件。您只需复制JSON响应并将其粘贴到插件中,它就会为您生成必要的类,这样您就可以更清楚地看到它。因此,您最终会得到:

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
)

如果你使用改进的方法,你的React会变成这样

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

然后,您只需在ViewModel中收集数据并将其显示在RecycleView中。

ar7v8xwq

ar7v8xwq2#

通过这种方式可以实现基本的解析。

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"))

                }

            }

相关问题