如何 select JSON 的特定部分并在 Moshi 改造中将其转换为列表
How to select specific part of JSON and convert it to a List in retrofit with Moshi
我正在从 API 改造中得到 JSON 打击,我只想 select production_companies 数组 并将其转换为 ProductionCompanie class 列表,如何使用 Moshi 而不使用嵌套 classes?
{
"backdrop_path": "/52AfXWuXCHn3UjD17rBruA9f5qb.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 18,
"name": "Drama"
}
],
"homepage": "http://www.foxmovies.com/movies/fight-club",
"id": 550,
"popularity": 40.054,
"poster_path": "/8kNruSfhk5IoE4eZOc4UpvDn6tq.jpg",
"production_companies": [
{
"id": 508,
"logo_path": "/7PzJdsLGlR7oW4J0J5Xcd0pHGRg.png",
"name": "Regency Enterprises",
"origin_country": "US"
},
{
"id": 711,
"logo_path": "/tEiIH5QesdheJmDAqQwvtN60727.png",
"name": "Fox 2000 Pictures",
"origin_country": "US"
},
{
"id": 20555,
"logo_path": "/hD8yEGUBlHOcfHYbujp71vD8gZp.png",
"name": "Taurus Film",
"origin_country": "DE"
},
{
"id": 54051,
"logo_path": null,
"name": "Atman Entertainment",
"origin_country": ""
}
],
"vote_count": 21181
}
这是我改造的Apis接口:
interface Apis {
@Headers("Content-Type: application/json")
@GET("/3/movie/550")
fun getData(@Query("api_key") key: String = apiKey): Call<List<ProductionCompanie>>
}
和我的模特:
@JsonClass(generateAdapter = true)
data class ProductionCompanie(
@Json(name = "id")
val id: Int,
@Json(name = "logo_path")
val picture: String,
@Json(name = "name")
val name: String
)
我最终使用了自定义适配器:
class ProductionCompanieListAdapter(private val moshi: Moshi) {
@FromJson
fun fromJson(value: JsonReader): List<ProductionCompanie>? {
val json = JSONObject(value.nextSource().readUtf8())
val jsonArray = json.getJSONArray("production_companies")
val type = Types.newParameterizedType(List::class.java, ProductionCompanie::class.java)
val adapter = moshi.adapter<List<ProductionCompanie>>(type)
return adapter.fromJson(jsonArray.toString())
}
@ToJson
fun toJson(value: List<ProductionCompanie>): String {
val type = Types.newParameterizedType(List::class.java, ProductionCompanie::class.java)
val adapter = moshi.adapter<List<ProductionCompanie>>(type)
return adapter.toJson(value)
}
}
我正在从 API 改造中得到 JSON 打击,我只想 select production_companies 数组 并将其转换为 ProductionCompanie class 列表,如何使用 Moshi 而不使用嵌套 classes?
{
"backdrop_path": "/52AfXWuXCHn3UjD17rBruA9f5qb.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 18,
"name": "Drama"
}
],
"homepage": "http://www.foxmovies.com/movies/fight-club",
"id": 550,
"popularity": 40.054,
"poster_path": "/8kNruSfhk5IoE4eZOc4UpvDn6tq.jpg",
"production_companies": [
{
"id": 508,
"logo_path": "/7PzJdsLGlR7oW4J0J5Xcd0pHGRg.png",
"name": "Regency Enterprises",
"origin_country": "US"
},
{
"id": 711,
"logo_path": "/tEiIH5QesdheJmDAqQwvtN60727.png",
"name": "Fox 2000 Pictures",
"origin_country": "US"
},
{
"id": 20555,
"logo_path": "/hD8yEGUBlHOcfHYbujp71vD8gZp.png",
"name": "Taurus Film",
"origin_country": "DE"
},
{
"id": 54051,
"logo_path": null,
"name": "Atman Entertainment",
"origin_country": ""
}
],
"vote_count": 21181
}
这是我改造的Apis接口:
interface Apis {
@Headers("Content-Type: application/json")
@GET("/3/movie/550")
fun getData(@Query("api_key") key: String = apiKey): Call<List<ProductionCompanie>>
}
和我的模特:
@JsonClass(generateAdapter = true)
data class ProductionCompanie(
@Json(name = "id")
val id: Int,
@Json(name = "logo_path")
val picture: String,
@Json(name = "name")
val name: String
)
我最终使用了自定义适配器:
class ProductionCompanieListAdapter(private val moshi: Moshi) {
@FromJson
fun fromJson(value: JsonReader): List<ProductionCompanie>? {
val json = JSONObject(value.nextSource().readUtf8())
val jsonArray = json.getJSONArray("production_companies")
val type = Types.newParameterizedType(List::class.java, ProductionCompanie::class.java)
val adapter = moshi.adapter<List<ProductionCompanie>>(type)
return adapter.fromJson(jsonArray.toString())
}
@ToJson
fun toJson(value: List<ProductionCompanie>): String {
val type = Types.newParameterizedType(List::class.java, ProductionCompanie::class.java)
val adapter = moshi.adapter<List<ProductionCompanie>>(type)
return adapter.toJson(value)
}
}