com.alibaba.fastjson.JSONException: exepct '[', but null, while Gson can parse correctly

yhived7q  于 2022-12-31  发布在  其他
关注(0)|答案(1)|浏览(348)

Env

Kotlin + Retrofit 2 + fastjson 1.1.68.android

Retrofit converter

https://github.com/alibaba/fastjson/blob/master/src/main/java/com/alibaba/fastjson/support/retrofit/Retrofit2ConverterFactory.java

Raw

{"Status":200,"ErrCode":401,"ErrMsg":"您的登录已过期,请重新登录","Data":null,"TotalCount":0}

Data model

@Keep
data class ResultModel<T>(val Status: Int, val ErrCode: Int, val ErrMsg : String = "",
                          val Data: T, val TotalCount : Int) {

    fun isSuccessful() = ErrCode == 0

}

@Keep
@Parcelize
data class Product(/**Not important so I omitted it**/) : Parcelable

Usage

The T I passed into is ResultModel<Array<Product>>

Problem

The Data was expected to get a Product array, but sometimes it may be null . Fastjson will run into an exception while Data is null rather than an empty null. I tried to switch to Gson converter and it solved my problem with the following expected result:

8nuwlpux

8nuwlpux1#

please provide exception stack detail information

相关问题