0

I have a JSON response of an API REST call that I am not pretty sure how should I deserialize...

{
.....
"date": "10-10-19", 
"rates": {
    "GBP" : 101.01,
    "EUR" : 102.01,
    "AUD" : 103.4,
    ......
    }
}

I would like to know How could I deserialize the "rates" object?. I think it was a Map object so using Gson I make the next POJO:

class POJO(
private val base: String,
private val date: Date,
private val rates: Rate
)

And my Rate class is

class Rate ( private val currency : Map <String, Double> )

It doesn't make any problem unless I try to use this Map in my class. When I try to access to this variable for example here :

view?.converterBinder!!.setCurrencyList(it.data!!.rates.currency)

currency is null because I think Gson doesn't know how to resolve it. I don't know if I had to deserialize it manually or there are any solution for this using Gson.

Any thoughts??

Sergio
  • 725
  • 1
  • 7
  • 20
  • the response itself it inappropriate. It is valid, but very badly-structured. For it to be comfortable to parse, it should be an array of currencies, not an object. You can create your own custom deserializer, which would iterate through keys of `rates` and store the values. – Vladyslav Matviienko Sep 23 '19 at 07:24
  • I know @VladyslavMatviienko it is a very strange response but I can´t modify it :( so I think I need to deserialize manually as you say. – Sergio Sep 23 '19 at 07:34
  • I am second to @VladyslavMatviienko comment, Your JSON is syntactically right but conceptually it's wrong. But though you can add customization to Map using following ways: https://www.baeldung.com/gson-json-to-map – Bipin Vayalu Sep 23 '19 at 07:53

1 Answers1

0

The provided Json is completely wrong, this is how it should be formatted

{
    "date": "10-10-19",
    "rates": {
        "GBP": 101.01,
        "EUR": 102.01
    }
}

Please check with https://jsonlint.com to confirm the validity of a Json.

So you have a json object with a String "date", then you have another json object called "rates" containing 2 numeric Doubles "GBP" and "EUR".

Each Json should be represented by a class, so to parse it create the following object containing the 2 classes

object Models {

    data class Rates(@SerializedName("GBP") val gbp: Double,
                     @SerializedName("EUR") val eur: Double)

    data class ExchangeRates(@SerializedName("date") val date: String,
                             @SerializedName("rates") val rates: Rates)
}

Now you pass the class ExchangeRates to Gson to deserialize your object and you should have all the data in place.

Kazikal
  • 156
  • 7
  • It is quite hard to notice what exactly is different in your *corrected* JSON. Please list what is wrong there – Vladyslav Matviienko Sep 23 '19 at 07:49
  • @VladyslavMatviienko it has the correct _double quotes_ and _colons_ – Kazikal Sep 23 '19 at 07:50
  • @kazikal I think you are still wrong around List because you have all the currency rated defined inside your POJO itself. How can it be a list? – Bipin Vayalu Sep 23 '19 at 07:50
  • please list it in the answer – Vladyslav Matviienko Sep 23 '19 at 07:50
  • @BipinVayalu Yes, I noticed to have made the mistake shortly after posting the answer. It is already corrected – Kazikal Sep 23 '19 at 08:19
  • You are right @Kazikal, i forget to validate before, but my question is in the way to know how could I deserialized this manually. I know I could iterate for all this rates (There are more, I only put two for the example) and get the keys and/or values but I don't know if I could iterate in rates and get an array because these rates are going to be showed in an recyclerview. – Sergio Sep 23 '19 at 16:45
  • @Kazikal Still it's wrong and Gson can't able to deserialize it automatically. Sergio, you need to register TypeAdpater when building Gson object. Please follow this link: https://stackoverflow.com/questions/41418160/how-to-make-a-custom-list-deserializer-in-gson – Bipin Vayalu Sep 24 '19 at 04:19