简体   繁体   中英

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 ?

You can just ignore them.

data class NearEarthObjects(@SerializedName("near_earth_objects") val nearEarthObjects: List<Objects>)
data class Objects(val id: String, val name: String)

If you then fetch the json you can just do this:

Gson().fromJson(yourJson, NearEarthObjects::class.java)

And you will get a list of all the objects name and id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM