json - Ignore some nested items in Gson Deserializing
问题描述
I want to deserialize NASA asteroids that I get from an API call in json format like this:
data class Asteroid(
val id: Int,
val name: String = "",
val meanDiameter: Int,
)
class Deserializer : ResponseDeserializable<Asteroid> {
override fun deserialize(content: String) = Gson().fromJson(content, Asteroid::class.java)
}
How can I ignore the first top items links
and page
and only deserialize near_earth_objects
in my Asteroid data class? And how can I access the nested items inside of near_earth_objects
?
解决方案
你可以忽略它们。
data class NearEarthObjects(@SerializedName("near_earth_objects") val nearEarthObjects: List<Objects>)
data class Objects(val id: String, val name: String)
如果你然后获取 json 你可以这样做:
Gson().fromJson(yourJson, NearEarthObjects::class.java)
您将获得所有对象名称和 ID 的列表。
推荐阅读
- java - 如何使用 KCL 使用来自 Kinesis Data Streams 的记录
- css - 从tornadofx中的标签中删除背景图像
- android - 如何仅删除某些行的当前字体
- linux - 如何使用另一个shell脚本一个接一个地运行多个shell脚本
- vue.js - VueJS:如何访问子函数中的父函数以有条件地呈现 css 类?
- node.js - 更新数组 Mongoose 中的嵌套对象
- elasticsearch - 更新 Elasticsearch 中的嵌套对象
- docker - `docker system prune -a` 实际上是做什么的?
- php - 使用逻辑运算符时检查 IF 语句的哪一部分是假/真
- xml - 使用 XmlService Google App 脚本解析 UTF-8-BOM xml 文件