[英]How to access a field in a nested json response with retrofit
I'm using retrofit to parse a json response but I'm not sure how to access a field in the nested response.我正在使用改造来解析 json 响应,但我不确定如何访问嵌套响应中的字段。 The response has a "results" field that leads to a nested list that has one of the fields I want ( https://api.jikan.moe/v3/search/anime?q=free ).响应有一个“结果”字段,它会导致一个嵌套列表,其中包含我想要的字段之一 ( https://api.jikan.moe/v3/search/anime?q=free )。
I have written these classes to handle the response:我编写了这些类来处理响应:
data class AnimeResponse(
val results: List<AnimeResultsResponse>
)
data class AnimeResponseNumbers(
@SerializedName("0") var zero: List<AnimeResultsResponse>,
@SerializedName("1") var one: List<AnimeResultsResponse>,
@SerializedName("2") var two: List<AnimeResultsResponse>,
@SerializedName("3") var three: List<AnimeResultsResponse>,
@SerializedName("4") var four: List<AnimeResultsResponse>,
@SerializedName("5") var five: List<AnimeResultsResponse>,
@SerializedName("6") var six: List<AnimeResultsResponse>,
@SerializedName("7") var seven: List<AnimeResultsResponse>,
@SerializedName("8") var eight: List<AnimeResultsResponse>,
@SerializedName("9") var nine: List<AnimeResultsResponse>,
@SerializedName("10") var ten: List<AnimeResultsResponse>,
)
data class AnimeResultsResponse(
var mal_id: String,
var url: String,
var image_url: String,
var title: String,
var airing: Boolean,
var synopsis: String,
var type: String,
var episodes: Int,
var score: Float,
var start_date: String,
var end_date: String,
var member: Int,
var rated: String
)
Then I manage the get request in another file:然后我在另一个文件中管理 get 请求:
interface APIService {
@GET
suspend fun getAnimeByCategory(@Url url:String):Response<AnimeResponse>
}
And this is how I'm supposed to get the response to show with retrofit and a coroutine:这就是我应该如何通过改造和协程获得响应:
private fun getRetrofit():Retrofit{
return Retrofit.Builder()
.baseUrl("https://api.jikan.moe/v3/search/anime?q=")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
private fun searchByName(query:String){
CoroutineScope(Dispatchers.IO).launch {
val call = getRetrofit().create(APIService::class.java).getAnimeByCategory("$query&limit=10")
val anime = call.body()
runOnUiThread{
if(call.isSuccessful) {
val images = anime?.results?.image_url ?: emptyList()
animeImages.clear()
animeImages.addAll(images)
adapter.notifyDataSetChanged()
}else{
showError()
}
}
hideKeyboard()
}
}
The problem is this line:问题是这一行:
val images = anime?.results?.image_url ?: emptyList()
I'm not sure how to get to "image_url", which is inside of the "results" field in the response and this way does not work.我不确定如何到达响应中“结果”字段内的“image_url”,这种方式不起作用。 How can I access it?我怎样才能访问它?
I think you should serialize all the fields in your model classes,我认为您应该序列化模型类中的所有字段,
for :为了 :
data class AnimeResponse(
val results: List<AnimeResultsResponse>
)
should become:应该变成:
data class AnimeResponse(
@SerializedName("results") val results: List<AnimeResultsResponse>
)
and for the AnimeResultsResponse
should be :对于AnimeResultsResponse
应该是:
data class AnimeResultsResponse(
@SerializedName("mal_id") var mal_id: String,
@SerializedName("url") var url: String,
@SerializedName("image_url") var image_url: String,
@SerializedName("title") var title: String,
@SerializedName("airing") var airing: Boolean,
@SerializedName("synopsis") var synopsis: String,
@SerializedName("type") var type: String,
@SerializedName("episodes") var episodes: Int,
@SerializedName("score") var score: Float,
@SerializedName("start_date") var start_date: String,
@SerializedName("end_date") var end_date: String,
@SerializedName("member") var member: Int,
@SerializedName("rated") var rated: String
)
also dont forget to update the url in your code because https://api.jikan.moe/v3/search/anime?q=
returns error 404也不要忘记更新代码中的 url,因为https://api.jikan.moe/v3/search/anime?q=
返回错误 404
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.