com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但 BEGIN_ARRAY
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
我是改造新手,在尝试解析我的应用程序内部 json 时偶然发现了一个问题。它看起来像这样:
{ "status": "success", "message": "Data selected from database", "data": [ { "id": "4622", "name": "xyz" } ] }
无论如何,它是 json 内部的一个 json,我的程序在这一行崩溃,当我只解析未嵌套的 json 的一部分时,它作品(状态和消息部分)。
该应用程序在此行崩溃:
viewModel.getPost()
我有两个数据 class 我使用,一个叫做 Post 并且在其中我保存状态和消息的值,以及数据,但是每当我尝试使用数据Post class 的一部分,程序崩溃了,我得到了
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但在第 1 行第 69 列路径 $.data 错误处 BEGIN_ARRAY。
我尝试在线查看,但没有找到任何适用于 kotlin 的解决方案。
这是我的数据 classes:
data class Post(
val status:String,
val message:String,
val data: Data)
data class Data (
val id : String ,
val name : String)
这是我的改造 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 回购:
class Repository {
suspend fun getPost(): Response<Post> {
return RetrofitInstance.api.getPost()
}}
以及我正在使用的 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
}}
这是我在 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()
})
有什么想法可以使数据部分正确,以便我可以在应用程序中使用它吗?
谢谢。
将 Post Class 中的数据类型更改为列表
{ "status": "success", "message": "Data selected from database", "data": [ { "id": "4622", "name": "xyz" } ] }
在此 json 数据中 正在接受 对象列表:
data": [ { "id": "4622", "name": "xyz" } ]
但是你有:
data class Post(
val status:String,
val message:String,
val data: Data)
将其转换为:
data class Post(
val status:String,
val message:String,
val data: List<Data)
我是改造新手,在尝试解析我的应用程序内部 json 时偶然发现了一个问题。它看起来像这样:
{ "status": "success", "message": "Data selected from database", "data": [ { "id": "4622", "name": "xyz" } ] }
无论如何,它是 json 内部的一个 json,我的程序在这一行崩溃,当我只解析未嵌套的 json 的一部分时,它作品(状态和消息部分)。 该应用程序在此行崩溃: viewModel.getPost()
我有两个数据 class 我使用,一个叫做 Post 并且在其中我保存状态和消息的值,以及数据,但是每当我尝试使用数据Post class 的一部分,程序崩溃了,我得到了 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但在第 1 行第 69 列路径 $.data 错误处 BEGIN_ARRAY。 我尝试在线查看,但没有找到任何适用于 kotlin 的解决方案。 这是我的数据 classes:
data class Post(
val status:String,
val message:String,
val data: Data)
data class Data (
val id : String ,
val name : String)
这是我的改造 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 回购:
class Repository {
suspend fun getPost(): Response<Post> {
return RetrofitInstance.api.getPost()
}}
以及我正在使用的 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
}}
这是我在 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()
})
有什么想法可以使数据部分正确,以便我可以在应用程序中使用它吗? 谢谢。
将 Post Class 中的数据类型更改为列表
{ "status": "success", "message": "Data selected from database", "data": [ { "id": "4622", "name": "xyz" } ] }
在此 json 数据中 正在接受 对象列表:
data": [ { "id": "4622", "name": "xyz" } ]
但是你有:
data class Post(
val status:String,
val message:String,
val data: Data)
将其转换为:
data class Post(
val status:String,
val message:String,
val data: List<Data)