-2

I'm trying to retrieve data from an api. The JSON response looks like this


{
    "result":"success",
    "documentation":"https://www.exchangerate-api.com/docs",
    "terms_of_use":"https://www.exchangerate-api.com/terms"
    "supported_codes":[
        ["AED","UAE Dirham"],
        ["AFN","Afghan Afghani"],
        ["ALL","Albanian Lek"],
        ["AMD","Armenian Dram"],
        ["ANG","Netherlands Antillian Guilder"],
        ["AOA","Angolan Kwanza"],
        ["ARS","Argentine Peso"],
        ["AUD","Australian Dollar"],
        ["AWG","Aruban Florin"],
        ["AZN","Azerbaijani Manat"],
        ["BAM","Bosnia and Herzegovina Convertible Mark"],
        ["BBD","Barbados Dollar"] etc. etc.
    ]
}

And this is the dataclass I have for it.

CurrencyResponse.kt


package com.example.currencyconverter.data

import com.squareup.moshi.Json

data class CurrencyResponse(
    @Json(name="supported_codes") var supported_codes: List<Codes>
) {
    data class Codes(
        @Json(name="0") var currency_code: String
    ) {

    }

}

Yet I'm still getting the error mentioned in the title. Any help is greatly appreciated

1
  • I'm not very familiar with Gson, but it doesn't seem right that you deserialize array (e.g. ["AED","UAE Dirham"]) to Codes object. You probably need something like this: var supported_codes: List<List<String>> or maybe use a custom serializer. Commented Oct 25, 2021 at 21:52

1 Answer 1

0

If possible, I think the API response scheme need a little modification. I think the supported_codes should contains list of object instead, i.e.

    "supported_codes":[
        {
             "currency_code": "AED",
             "currency_name": "UAE Dirham"
        },
        {
             "currency_code": "AFN",
             "currency_name": "Afghan Afghani"
        },
        ... etc ...
    ]

Then, in your kotlin code, you need to modify the Codes data class to:

data class Codes(
   @Json(name="currency_code") val currencyCode: String,
   @Json(name="currency_name") val currencyName: String,
)
Sign up to request clarification or add additional context in comments.

4 Comments

But here's the thing, the array entries don't have a name so can I write "currency_code" and "currency_name" as names?
Unfortunately, it requires to change the API response schema as well. But, in my opinion, putting array of object inside supported_codes can help to ease the client side to identify the property passed, instead array of array
I changed it to this ` data class CurrencyResponse( @Json(name ="supported_codes") val supportedCodes: List<List<String>> ){ } ` The app no longer crashes but I'm getting a null response
Or, you can try to parse supportedCodes as answered in stackoverflow.com/a/17974337

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.