java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 45 path $.USD (Kotlin)
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:You will have to fix your data class to correctly repesent the JSON structure.
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.