Home > OS >  Get SerializedName Values
Get SerializedName Values

Time:11-01

I am starting to learn kotlin and I need some help.

So my question is how can I get the serializedName I defined in another class ?

I am using this line to get all the data:

val responseObject = Gson().fromJson(response, GifSearchResult::class.java)
Log.e("Data",""   responseObject.data)

The log is:

E/Data: GifDetails(type=gif, id=13Cmju3maIjStW, url=https://giphy.com/gifs/13Cmju3maIjStW, title=Biting Nails Reaction GIF by SpongeBob SquarePants, images=Images(original=Urls(width=500, height=373, url=https://media0.giphy.com/media/13Cmju3maIjStW/giphy.gif?cid=8d79dd67s0dkoxp1m53kmunlydxrnnuijiv917zp7qdr6tnd&rid=giphy.gif&ct=g, size=194803, mp4Url=https://media0.giphy.com/media/13Cmju3maIjStW/giphy.mp4?cid=8d79dd67s0dkoxp1m53kmunlydxrnnuijiv917zp7qdr6tnd&rid=giphy.mp4&ct=g, mp4Size=52101, webpUrl=https://media0.giphy.com/media/13Cmju3maIjStW/giphy.webp?cid=8d79dd67s0dkoxp1m53kmunlydxrnnuijiv917zp7qdr6tnd&rid=giphy.webp&ct=g, webpSize=115414), fixedWidth=Urls(width=200, height=149, url=https://media0.giphy.com/media/13Cmju3maIjStW/200w.gif?cid=8d79dd67s0dkoxp1m53kmunlydxrnnuijiv917zp7qdr6tnd&rid=200w.gif&ct=g, size=47897, mp4Url=https://media0.giphy.com/media/13Cmju3maIjStW/200w.mp4?cid=8d79dd67s0dkoxp1m53kmunlydxrnnuijiv917zp7qdr6tnd&rid=200w.mp4&ct=g..............

I need only the urls without anything else.

This is GifSearchResult.class:

data class GifSearchResult(
    @Expose
    @SerializedName("data")
    var data: List<GifDetails>,

    @Expose
    @SerializedName("pagination")
    var pageInformation: PageInformation,
    ) {

    data class PageInformation(
        @Expose
        @SerializedName("total_count")
        var totalCount : Int = 0,
        @Expose
        @SerializedName("count")
        var count: Int = 0,
        @Expose
        @SerializedName("offset")
        var offset: Int = 0
    )
    data class GifDetails(
        @Expose
        @SerializedName("type")
        var type: String = "",
        @Expose
        @SerializedName("id")
        var id: String = "",
        @Expose
        @SerializedName("url")
        var url: String = "",
        @Expose
        @SerializedName("title")
        var title: String = "",
        @Expose
        @SerializedName("images")
        var images: Images = Images()
    ) {
        data class Images(
            @Expose
            @SerializedName("original")
            var original: Urls = Urls(),
            @Expose
            @SerializedName("fixed_width")
            var fixedWidth: Urls = Urls(),
            @Expose
            @SerializedName("fixed_width_downsampled")
            var downSampledFixedWidth: Urls = Urls(),
            @Expose
            @SerializedName("downsized")
            var downsized: Urls = Urls()
        )
        data class Urls(
            @Expose
            @SerializedName("width")
            var width: String = "",
            @Expose
            @SerializedName("height")
            var height: String = "",
            @Expose
            @SerializedName("url")
            var url: String = "",
            @Expose
            @SerializedName("size")
            var size: String = "",
            @Expose
            @SerializedName("mp4")
            var mp4Url: String = "",
            @Expose
            @SerializedName("mp4_size")
            var mp4Size: String = "",
            @Expose
            @SerializedName("webp")
            var webpUrl: String = "",
            @Expose
            @SerializedName("webp_size")
            var webpSize: String = ""
        )
        data class Url(
            @Expose
            @SerializedName("url")
            var Url: String = ""
        )

    }

}

I can only access pagination and data, without the subtitles.

Any help and explanation will be appreciated!

Thanks!

CodePudding user response:

So I managed to do it by running a loop:

                for (i in 0 until responseObject.pageInformation.count) {

                   Log.e("Data",""   responseObject.data[i].images.downsized.url)

                }

CodePudding user response:

Typically, in such cases we use map(), flatMap() or combination of both. They take a collection of items and create another collection by mapping/transforming each item to a new value. In your case it is as simple as:

responseObject.data.map { it.url }

If we need to ignore empty urls ("") then we can additionally filter them out:

responseObject.data
    .map { it.url }
    .filter { it.isNotEmpty() }

Alternatively, we can do the same in a single step:

responseObject.data.mapNotNull { it.url.ifEmpty { null } }
  • Related