访问嵌套的 Json Kotlin

huangapple go评论51阅读模式
英文:

Access to Nested Json Kotlin

问题

我不知道如何从嵌套的Json中获取数据

{
   "results":[
      {
         "id":1,
         "name":"Rick Sanchez",
         "status":"Alive",
         "species":"Human",
         "type":"",
         "gender":"Male"
      }
   ]
}

Json看起来像上面这样,我想要访问name变量。

我的代码:
数据类:

data class Movie(
    @Json(name = "results") val results: List<MovieDetail>
)
data class MovieDetail(
    @Json(name = "name") val name: String
)

ApiService:

private const val BASE_URL = "https://rickandmortyapi.com/api/"

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()

interface MovieApiService {
    @GET("character")
    suspend fun getMovies(): List<Movie>
}

object MovieApi {
    val retrofitService : MovieApiService by lazy {
        retrofit.create(MovieApiService::class.java)
    }
}

ViewModel:

private val _status = MutableLiveData<String>()
val status: LiveData<String> = _status

init {
    getMovies()
}

private fun getMovies() {
    viewModelScope.launch {
        val listResult = MovieApi.retrofitService.getMovies()
        _status.value = "Success: ${listResult.size} names retrieved"
    }
}

对于普通的Json没有问题,但我不知道如何访问这些嵌套的变量,我认为我必须从数据类中使用"results"变量,但我不知道在哪里和如何使用。
在运行应用程序时,我收到错误:Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $

英文:

I don't know how to get data from nested Json

{
  
   &quot;results&quot;:[
      {
         &quot;id&quot;:1,
         &quot;name&quot;:&quot;Rick Sanchez&quot;,
         &quot;status&quot;:&quot;Alive&quot;,
         &quot;species&quot;:&quot;Human&quot;,
         &quot;type&quot;:&quot;&quot;,
         &quot;gender&quot;:&quot;Male&quot;,

Json looks like above, i want to get access to name variable.
My code:
Data class:

data class Movie(
    @Json(name = &quot;results&quot;) val results: List&lt;MovieDetail&gt;
)
data class MovieDetail(
    @Json(name = &quot;name&quot;) val name: String

)

ApiService:

private const val BASE_URL = &quot;https://rickandmortyapi.com/api/&quot;

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()

interface MovieApiService {
    @GET(&quot;character&quot;)
    suspend fun getMovies(): List&lt;Movie&gt;
}

object MovieApi {
    val retrofitService : MovieApiService by lazy {
        retrofit.create(MovieApiService::class.java)
    }
}

And ViewModel:

  private val _status = MutableLiveData&lt;String&gt;()
    val status: LiveData&lt;String&gt; = _status

    init {
        getMovies()
    }

    private fun getMovies() {
        viewModelScope.launch {
            val listResult = MovieApi.retrofitService.getMovies()
            _status.value = &quot;Success: ${listResult.size} names retrieved&quot;
        }
    }

For plain Json there is no problem but i don't know how to get access to this nested variables, i think that i have to use "results" variable from data class but i don't know where and how.
During running app i've got error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $

答案1

得分: 1

你应该将以下代码更改为:

@GET("character")
suspend fun getMovies(): Movie

你正在接收一个对象,而不是对象的列表。

英文:

You should change

@GET(&quot;character&quot;)
suspend fun getMovies(): List&lt;Movie&gt;

To:

@GET(&quot;character&quot;)
suspend fun getMovies(): Movie

You are receiving object and not list of objects

huangapple
  • 本文由 发表于 2023年2月20日 00:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定