question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 45 path $.USD (Kotlin)

See original GitHub issue

I have Problem “java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 45 path $.USD” I did some research on this issue, but there was no result.

My Code Build

Interface

 interface ApiService {
    @Headers("Content-Type: application/json")
     @GET("today.json")
   fun getData() : Call<Data>
 }

Data Class

data class Data(
 @SerializedName("Update_Date")
 val Update_Date : String,
 @SerializedName("USD")
 var USD: List<USDList>,
)

List<USDList>

data class USDList ( 
    @SerializedName("Buying")
    @Expose
    var buying: String? = null,

    @SerializedName("Selling")
    @Expose
    var selling: String? = null,

     @SerializedName("Type")
     @Expose
      var type: String? = null,

      @SerializedName("Name")
      @Expose
       var name: String? = null)

MainActivity

val retrofit = Retrofit.Builder()
           .addConverterFactory(GsonConverterFactory.create())
           .baseUrl("https://finans.truncgil.com/v2/")
           .build()

        val retrofitCreate = retrofit.create(ApiService::class.java)
        retrofitCreate.getData().enqueue(object : Callback<Data>{
            override fun onResponse(call: Call<Data>, response: Response<Data>) {
                println("Datas : ${response.body().toString()}")
                showData(response.body())
            }

            private fun showData(body: Data?) {
                recylerViewDöviz.apply {
                    adapter = body.let { DövizRecylerView(it!! , this@DovizActivity) }
                    layoutManager = LinearLayoutManager(this@DovizActivity)
                }
            }

            override fun onFailure(call: Call<Data>, t: Throwable) {
                Log.e(TAG , t.localizedMessage.toString())
            }

        })

How to fix problem? I don’t know if it is forbidden to share links https://finans.truncgil.com/v2/today.json I get the json data from here

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Marcono1234commented, Feb 23, 2021

As described by the exception message your data class makes Gson expect a JSON array (because you are using List<...>), but the actual JSON data is using a JSON object:

"USD":{"Buying":"7,0253","Selling":"7,0380","Type":"Exchange","Name":"ABD DOLARI"}

You will have to fix your data class to correctly repesent the JSON structure.

1reaction
mattwelkecommented, Mar 10, 2022

Am facing the same exception

This sounds like a problem with using the library rather than with the library itself, and Stack Overflow would be a better venue.

We’ve already established that it isn’t an issue with the library causing the issue. That means it’s an issue with how you’re using the library. Stack Overflow is good for this. They’ll expect a minimal reproduction of the issue, so be sure to include some code showing how to cause that exception.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 ...
I'm getting below error. Failed to parse JSON due to: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY ...
Read more >
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 ...
Android : Retrofit2 Android: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ [ Beautify Your Computer ...
Read more >
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 ...
Coding example for the question Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 $-kotlin.
Read more >
Expected BEGIN OBJECT but was BEGIN ARRAY at line 1 ...
isSuccessful()){ assert response.body() != null; String name = response.body().getData().getFull_name();. android · java.
Read more >
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 ...
... and out of the blue, syncing the project failed with "java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found