Android / Kotlin:GSON无法正确解析某些字段JSON

时间:2019-07-10 18:09:46

标签: android json kotlin gson rx-java2

GSON无法正确解析我的JSON,并且仅对某些字段返回null
确实,我正确地获得了doctorrppscode中的每一个,但是我有null代表lifenIdfirstNamelastName
我有一个JSON,它是doctor的列表,看起来像这样:

[
    {
        "lifenId": "238536",
        "rpps": "10002732211",
        "firstName": "Yolande ",
        "lastName": "Couffinhal",
        "email": null,
        "postalCode": null,
        "city": null,
        "address": null,
        "code": "Médecin Neuro-psychiatrie ",
        "phone": null
    },
...
]

我确定我的JSON是正确的,因为我收到的JSON正好是我在此处发布的那个,因此这些值都不为空(例如,我不是在讲电话null中的JSON
我试图手动解析JSON来获取firstName和lastName起作用(显然是因为我有一个不错的JSON,但我想确定这一点)

有我的data class Doctor

data class Doctor(var phone: String,
                  var code: String,
                  var address: MutableList<String>,
                  var email: String,
                  var rpps: String,
                  var city: String,
                  var lastName: Any,
                  var firstName: Any,
                  var lifenId: String,
                  var postalCode: String) {
}  

我的请求:

fun getDoctorsByName(name: String, token: String): Single<List<Doctor>> {
        val url = "lifen/name/" + name
        return repository.getDoctorsByName(url, token).compose(schedulerProvider.getSchedulersForSingle())
    }  

我如何处理退货:

disposable.add(viewModel.getDoctorsByName(search_bar.query.toString(), activity.token)
                    .doOnError {
                        Toast.makeText(activity, it.message, Toast.LENGTH_SHORT).show()
                    }
                    .subscribe(
                        { doctors ->
                            /*val list = ArrayList<Doctor>()
                            val doc = Doctor("", "","","","",doctors[0]["lastName"] as String,doctors[0]["firstName"] as String,"","") parse manually here
                            list.add(doc)
                            adapter.updateList(list)*/
                            adapter.updateList(doctors)
                            Toast.makeText(activity, "LIST COMPLETED", Toast.LENGTH_SHORT).show()
                        }, Throwable::printStackTrace
                    )

                )

2 个答案:

答案 0 :(得分:0)

您的数据类中的名字和姓氏为FileSystemWatcher fsw = new FileSystemWatcher(@"C:\One\Two\Three"); fsw.EnableRaisingEvents = true; 。将它们更改为Any,您应该会很好。另外,如果有时它们为空,则将它们标记为可空,并在访问它们时进行检查。

答案 1 :(得分:0)

我发现了这个错误,当我创建GSON客户端时,我按如下所示放置了一个过滤器:

fun provideGson(): Gson {
        return GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .create()
    }

所以我刚刚删除了此过滤器,所以这就是在lifenIdfirstNamelastNameGSON上没有得到这些字段的原因。