英文:
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
{
  
   "results":[
      {
         "id":1,
         "name":"Rick Sanchez",
         "status":"Alive",
         "species":"Human",
         "type":"",
         "gender":"Male",
Json looks like above, i want to get access to name variable.
My code:
Data class:
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)
    }
}
And 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"
        }
    }
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("character")
suspend fun getMovies(): List<Movie>
To:
@GET("character")
suspend fun getMovies(): Movie
You are receiving object and not list of objects
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论