A
A
Artemy Strukov2021-03-07 11:20:36
Android
Artemy Strukov, 2021-03-07 11:20:36

How to parse JSON consisting only of objects, not a list?

You need to parse this JSON . My version with the usual map returns null, and I don't want to create separate variables for each object (as the auto-parser suggests to me).
My version:

data class ModelList(
    val map: Map<String, String>
)

Call via Retrofit :
retrofit = Retrofit.Builder()
            .baseUrl("https://openexchangerates.org/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

retrofit!!.create(Api::class.java).currencies().enqueue(object : Callback<ModelList>{
            override fun onResponse(call: Call<ModelList>, response: Response<ModelList>) {
                if (response.body() != null){
//                    rec_list.apply {
//                        layoutManager = LinearLayoutManager([email protected])
//                    }
                }else{
                    showAlert("Error Get: "+ response.code(), response.message(), [email protected])
                }
            }

            override fun onFailure(call: Call<ModelList>, t: Throwable) {
                showAlert("Error Get: Fail", t.message.toString(), [email protected])
            }
        })

Interface:
interface Api {
    @GET("currencies.json")
    fun currencies(): Call<ModelList>
}

Result:
60448c3f1dc51827711819.png
Auto-parser suggestion:
data class Test(
    var AED: String,
    var AFN: String,
    var ALL: String,
    var AMD: String,
    var ANG: String,
    var AOA: String,
    var ARS: String,
    var AUD: String,
    var AWG: String,
    var AZN: String,
    var BAM: String,
    var BBD: String,
    var BDT: String,
    var BGN: String,
    var BHD: String,
    var BIF: String,
    var BMD: String,
    var BND: String,
    var BOB: String,
    var BRL: String,
    var BSD: String,
    var BTC: String,
    var BTN: String,
    var BWP: String,
    var BYN: String,
    var BZD: String,
    var CAD: String
    .......

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacen11, 2021-03-08
@Jacen11

GsonConverterFactory is not suitable, he wants a model, he needs his own with a map. I was looking for something like this but didn't find it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question