[英]Error in parsing json with class Kotlin Android
嗨,我正在尝试用 kotlin 解析 JSON
下面是我的 json 代码
[{
"module":"1",
"books":[{"name":"bookname1","authors":"author1, author 2"},
{"name":"bookname2","authors":"author1, author 2"},
{"name":"bookname3","authors":"author1, author 2"}]
},
{
"module":"2",
"books":[{"name":"bookname1","authors":"author1, author 2"},
{"name":"bookname2","authors":"author1, author 2"},
{"name":"bookname3","authors":"author1, author 2"}]
},
{
"module":"3",
"books":[{"name":"bookname1","authors":"author1, author 2"},
{"name":"bookname2","authors":"author1, author 2"},
{"name":"bookname3","authors":"author1, author 2"}]
},
{
"module":"4",
"books":[{"name":"bookname1","authors":"author1, author 2"},
{"name":"bookname2","authors":"author1, author 2"},
{"name":"bookname3","authors":"author1, author 2"}]
},
{
"module":"5",
"books":[{"name":"bookname1","authors":"author1, author 2"},
{"name":"bookname2","authors":"author1, author 2"},
{"name":"bookname3","authors":"author1, author 2"}]
}]
请注意,此 json 响应以数组开头
这是我的 class 来解析它
class SemdetailsPArser {
@SerializedName("module")
@Expose
var module: String? = null
@SerializedName("books")
@Expose
var books: List<Book>? = null
}
class Book {
@SerializedName("name")
@Expose
var name: String? = null
@SerializedName("authors")
@Expose
var authors: String? = null
}
这是我的代码
//interface
interface SemdetailsFetcher {
@GET("test/json/sub1.json")
fun getCurrentSemData(): Call<SemdetailsPArser>
}
这是我的活动代码
fun getCurrentData() {
val retrofit = Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(SemdetailsFetcher::class.java)
val call = service.getCurrentSemData()
call.enqueue(object : Callback, retrofit2.Callback<SemdetailsPArser> {
override fun onResponse(
call: retrofit2.Call<SemdetailsPArser>?,
response: retrofit2.Response<SemdetailsPArser>?
) {
// val thisthig = response?.body();
println("here 1 ${response?.body().toString()}")
}
override fun onFailure(call: Call?, e: IOException?) {
println("here 2")
}
override fun onFailure(call: retrofit2.Call<SemdetailsPArser>?, t: Throwable?) {
println("here 3 $t")
}
override fun onResponse(call: Call, response: Response) {
if (response.code() == 200) {
println("secodn success")
val sampleResp = response.body()!!
println(sampleResp)
}
}
})
}
我收到了这个错误
here 3 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
我知道这可能与我的解析 class 有关
在这里,我得到了一组 json 信息,我用另一个 json 尝试了相同的代码
{
"person": {
"name": "Don",
"age": 35
},
"books": [{
"id": 800,
"name": "book 1",
"description": "clear sky",
"icon": "01n"
},
{
"id": 801,
"name": "book 2",
"description": "clear sky 1",
"icon": "01N"
}
],
"city": "bgvnslsl",
"id": 1851632,
"bname": "abcd",
"code": 200
}
当我更改解析 class 和接口时,这工作得很好
我的问题是我不知道如何编写 class 来解析以数组开头的 json 响应
您期待SemdetailsPArser
列表,因此您应该将返回类型定义为SemdetailsPArser
列表
这应该可以解决问题。
interface SemdetailsFetcher {
@GET("test/json/sub1.json")
fun getCurrentSemData(): Call<List<SemdetailsPArser>>
}
您还需要在代码的其他部分进行更改。
您收到的错误意味着您正在尝试解析 JSON 数组,认为它应该是 JSON object。 JSON 数组是这些[]
之间的东西,而 JSON object 在像这样的大括号中{}
。 所以你的第一个 JSON 对应于List<Module>
之类的东西,它不是 object,而是它们的列表。 每个模块都有一个书籍列表。
所以都说应该是这样的
interface SemdetailsFetcher {
@GET("test/json/sub1.json")
fun getCurrentSemData(): Call<List<SemdetailsPArser>>
}
顺便说一句,如果您正确定义 POJO,则不需要所有注释。
创建 SemdetailsPARser class
data class SemdetailsPArser( val books: List<Book>, val module: String )
接下来创建书 class
data class Book(
val authors: String,
val name: String
)
界面中的下一步(SemdetailsFetcher)
interface SemdetailsFetcher {
@GET("test/json/sub1.json")
fun getCurrentSemData(): Call<List<SemdetailsPArser>>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.