簡體   English   中英

使用 class Kotlin Android 解析 json 時出錯

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM