[英]com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
I am new to retrofit and I stumbled upon a problem when trying to parse json inside of my application.我是 retrofit 的新手,在我的应用程序内部尝试解析 json 时偶然发现了一个问题。 It looks something like this:
它看起来像这样:
{ "status": "success", "message": "Data selected from database", "data": [ { "id": "4622", "name": "xyz" } ] }
Anyways, its a json inside of json, and my program crashes at this exact line, when I parse just part of the json that isn't nested, it works (status and message part).无论如何,它是 json 内部的 json,当我仅解析未嵌套的 json 的一部分时,我的程序在这一行崩溃,它可以工作(状态和消息部分)。 The app crashes at this exact line: viewModel.getPost()
应用程序在这一行崩溃:viewModel.getPost()
I have two data classes that I use, one is called Post and in it I hold the values of status and message, and data, but whenever I try to use the Data part of the Post class, the program crashes and I get com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 69 path $.data error.我使用了两个数据类,一个称为 Post,在其中我保存状态和消息的值以及数据,但是每当我尝试使用 Post class 的数据部分时,程序崩溃并且我得到 com。 google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 69 列路径 $.data 错误处为 BEGIN_ARRAY。 I tried to look online but haven't found any solutions that worked for me in kotlin.
我试图在网上查找,但在 kotlin 中没有找到任何适合我的解决方案。 Here are my data classes:
这是我的数据类:
data class Post(
val status:String,
val message:String,
val data: Data)
data class Data (
val id : String ,
val name : String)
Here is my retrofit api:这是我的 retrofit api:
object RetrofitInstance {
private val retrofit by lazy{
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val api:SimpleApi by lazy{
retrofit.create(SimpleApi::class.java)
}}
interface SimpleApi {
@GET("token/username")
suspend fun getPost(): Response<Post>}
Post repo:发布回购:
class Repository {
suspend fun getPost(): Response<Post> {
return RetrofitInstance.api.getPost()
}}
And viewModel files I am using:我正在使用的 viewModel 文件:
class MainViewModel(private val repository:Repository): ViewModel() {
val myResponse:MutableLiveData<Response<Post>> = MutableLiveData()
fun getPost(){
viewModelScope.launch {
val response=repository.getPost()
myResponse.value=response
}
}}
class MainViewModelFactory(private val repository: Repository):ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T{
return MainViewModel(repository) as T
}}
This is the code in which im using the above in the activity:这是我在活动中使用上述内容的代码:
val repository=Repository()
val viewModelFactory=MainViewModelFactory(repository)
viewModel=ViewModelProvider(this,viewModelFactory).get(MainViewModel::class.java)
viewModel.getPost()
viewModel.myResponse.observe(this, Observer { response->
if(response.isSuccessful && response.body()!=null){
Log.d("Response", response.body().toString())
Log.d("Response",response.body()?.message.toString())
Toast.makeText(this, response.body()?.data!!.id,Toast.LENGTH_LONG).show()
}
else{
Log.d("Error response",response.errorBody().toString())
Toast.makeText(this,"error",Toast.LENGTH_LONG).show()
}
//Toast.makeText(this, response.data.id,Toast.LENGTH_LONG).show()
})
Any ideas what could I do to get the Data part right so I can work with it in the application?有什么想法可以让数据部分正确,以便我可以在应用程序中使用它吗? Thank you.
谢谢你。
Change type of data in Post Class to List将 Post Class 中的数据类型更改为 List
{ "status": "success", "message": "Data selected from database", "data": [ { "id": "4622", "name": "xyz" } ] }
in this json data is accepting list of object:在此 json数据中接受object 列表:
data": [ { "id": "4622", "name": "xyz" } ]
but you are having:但你有:
data class Post(
val status:String,
val message:String,
val data: Data)
convert it to:将其转换为:
data class Post(
val status:String,
val message:String,
val data: List<Data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.