当gson显然是数组时,为什么gson认为我的json feed是一个对象?

时间:2019-11-23 15:09:10

标签: android kotlin gson

我有一个android程序正在读取的JSON feed:

[   {     “ AddressLine1”:“公园路91号”,     “ AddressLine2”:“萨顿科尔菲尔德”,     “ AddressLine3”:“伯明翰”,     “ Bio”:“通过将地道的口味和优质的服务辛勤耕combination,您一定会一次又一次地在亲朋好友中享受Asian Grill。”,     “ CompanyDescription”:“印度餐厅”,     “ CompanyID”:2     “ CompanyName”:“测试测试”,     “ CoverFileExtension”:“ jpg”,     “ CoverFileName”:“ cover_asiangrill”,     “ Deliveroo”:null,     “ Facebook”:“ 186939334708528”,     “ Instagram”:null,     “ JustEat”:null,     “ LinkedIn”:null,     “ LogoFileExtension”:“ png”,     “ LogoFileName”:“ logo_asiangrill”,     “ OpeningHours”:“ 17:00-23:00”,     “邮政编码”:“ B73 XXX”,     “电话”:“ 0121 354 xxx”,     “ Twitter”:null,     “ UberEats”:null,     “不可用”:false,     “网站”:null   },   {     “ AddressLine1”:“ Gracechurch购物中心”,     “ AddressLine2”:“萨顿科尔菲尔德”,     “ AddressLine3”:null,     “生物”:“鲜榨果汁,新鲜水果冰沙,蛋白质冰沙和可丽饼!”,     “ CompanyDescription”:“ Smoothie and Juice Bar”,     “ CompanyID”:6     “ CompanyName”:“ Juiceys”,     “ CoverFileExtension”:“ jpg”,     “ CoverFileName”:“ cover_juiceys”,     “ Deliveroo”:null,     “ Facebook”:“ squeezeandgrinduk”,     “ Instagram”:“ juiceysuk”,     “ JustEat”:“ juiceys-sutton”,     “ LinkedIn”:“”,     “ LogoFileExtension”:“ jpg”,     “ LogoFileName”:“ logo_test”,     “ OpeningHours”:“ 09:00-17:30”,     “邮政编码”:“ B72 XXX”,     “电话”:“ 0121 355 XXX”,     “ Twitter”:null,     “ UberEats”:null,     “不可用”:false,     “网站”:null   } ]

下面的我的kotlin代码很好地获取了json字符串:

fun fetchJson(){
        val url = Singleton.getUrlBase() + Singleton.getUrlPlacesBasic() + "400"
        val request = Request.Builder().url(url).build()
        val client = OkHttpClient()
        client.newCall(request).enqueue(object: Callback {
            override fun onResponse(call: Call, response: Response) {
                val body  = response?.body?.string()

                val gson = GsonBuilder().create()
                try {
                    val placesFeed = gson.fromJson(body, CompanyInfoBasicFeed::class.java)
                }
                catch(e: Exception){
                    val errString = e.toString()
                }
            }

            override fun onFailure(call: Call, e: IOException) {
                println("Failed to execute")
            }
        })
    }

但不幸的是我得到了错误: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

我的数据类如下:

//JsonObjects
class CompanyInfoBasicFeed(val companiesInfoBasic: List<CompanyInfoBasic>)

data class CompanyInfoBasic(val CompanyID: Int,val CompanyName: String?,val PostCode: String?,val
                            CompanyDescription: String?,val LogoFileName: String?,val CoverFileName: String?,val
                            LogoFileExtension: String?,val CoverFileExtension: String?,val
                            AddressLine1: String?,val AddressLine2: String?,val AddressLine3: String?,val Telephone: String?,
                            val Facebook : String?,val Twitter: String?,val Instagram: String?,val LinkedIn: String?,val JustEat: String?,val
                            Deliveroo: String?,val UberEats: String?,val Website: String?,val
                            Bio: String?,val OpeningHours : String?,val Unavailable: Boolean )

它认为我要解析的对象不是数组/列表吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

CompanyInfoBasicFeed不是ListCompanyInfoBasicFeed 包含一个List,但这需要具有companiesInfoBasic属性的外部JSON对象才能匹配您的构造函数属性。您的JSON对象没有这样的companiesInfoBasic属性。

您将需要让Gson解析List<CompanyInfoBasic>并摆脱CompanyInfoBasicFeed。或者,可能将CompanyInfoBasicFeed扩展为List<CompanyInfoBasic>