Android Kotlin RxJava Retrofit - Json 具有预期属性列表的对象 BEGIN_ARRAY 但 BEGIN_OBJECT
Android Kotlin RxJava Retrofit - Json object that has lists as attributes expected BEGIN_ARRAY but was BEGIN_OBJECT
我从 API 接收到一个 Person 对象,该对象具有另一个对象的列表作为其属性之一。我在日志中收到以下错误:"Expexted BEGIN_ARRAY but was BEGIN_OBJECT at path $"。所以 JSON 没有正确解析列表。我使用 Moshi 进行 JsonParsing。
人JSON对象:(实际项目中有多个人)
{
"personId": 1,
"personName": "Bert",
"personAge": 19,
"isFemale": "false",
"dogs": [{
"dogId": 1,
"dogName": "Fred",
"isFemale": "false"
}, {
"dogId": 2,
"dogName": "Laika",
"isFemale": "true"
}],
"birthDate": "2000-06-28T00:00:00"
}
狗数据模型class:
data class Dog(
@Json(name = "dogId")
val dogId: Int,
@Json(name = "dogName")
val name: String,
@Json(name = "isFemale")
val isFemale: Boolean,
)
Person 数据模型class:
data class Person (
@Json(name = "personId")
val personId: Int,
@Json(name = "personName")
val name: String,
@Json(name = "personAge")
val age: Int,
@Json(name = "isFemale")
val isFemale: Boolean,
@Json(name = "dogs")
val dogs: List<Dog>
@Json(name = "birthDate")
val birthDate: GregorianCalendar
)
ApiInterface:(注意 Person 列表,因为它总是 returns 一个 Person 对象的列表)
@GET("persons/{personId}")
fun getPerson(@Path("personId") id: Int): Observable<List<Person>>
个人资料库:
class PersonRepository @Inject constructor(val apiInterface: ApiInterface) {
fun getPerson(personId: Int): Observable<Person>{
return apiInterface.getPerson(personId)
.doOnNext { value -> Log.d("PERSON_LOG_TAG", "onNext: value=$value") }
.flatmap { response ->
if(response.size == 1) {
Observable.just(response[0])
} else {
Observable.error(Throwable("Something went wrong")
}
}
}
}
PERSON_LOG_TAG 给出 "expected the BEGIN_ARRAY but was BEGIN_OBJECT error"。知道如何解决吗?这通常是通过创建一个外部 JSON class 来解决的,比如 PersonResponse 并且它有一个真实的列表 class。但是我不知道如何使用另一个对象中的列表来完成它。
如果 API 按约定创建,而不是 persons/{personId}
应该 return Person 对象而不是 Persons 列表。所以我假设你应该更换
@GET("persons/{personId}")
fun getPerson(@Path("personId") id: Int): Observable<List<Person>>
到
@GET("persons/{personId}")
fun getPerson(@Path("personId") id: Int): Observable<Person>
我从 API 接收到一个 Person 对象,该对象具有另一个对象的列表作为其属性之一。我在日志中收到以下错误:"Expexted BEGIN_ARRAY but was BEGIN_OBJECT at path $"。所以 JSON 没有正确解析列表。我使用 Moshi 进行 JsonParsing。
人JSON对象:(实际项目中有多个人)
{
"personId": 1,
"personName": "Bert",
"personAge": 19,
"isFemale": "false",
"dogs": [{
"dogId": 1,
"dogName": "Fred",
"isFemale": "false"
}, {
"dogId": 2,
"dogName": "Laika",
"isFemale": "true"
}],
"birthDate": "2000-06-28T00:00:00"
}
狗数据模型class:
data class Dog(
@Json(name = "dogId")
val dogId: Int,
@Json(name = "dogName")
val name: String,
@Json(name = "isFemale")
val isFemale: Boolean,
)
Person 数据模型class:
data class Person (
@Json(name = "personId")
val personId: Int,
@Json(name = "personName")
val name: String,
@Json(name = "personAge")
val age: Int,
@Json(name = "isFemale")
val isFemale: Boolean,
@Json(name = "dogs")
val dogs: List<Dog>
@Json(name = "birthDate")
val birthDate: GregorianCalendar
)
ApiInterface:(注意 Person 列表,因为它总是 returns 一个 Person 对象的列表)
@GET("persons/{personId}")
fun getPerson(@Path("personId") id: Int): Observable<List<Person>>
个人资料库:
class PersonRepository @Inject constructor(val apiInterface: ApiInterface) {
fun getPerson(personId: Int): Observable<Person>{
return apiInterface.getPerson(personId)
.doOnNext { value -> Log.d("PERSON_LOG_TAG", "onNext: value=$value") }
.flatmap { response ->
if(response.size == 1) {
Observable.just(response[0])
} else {
Observable.error(Throwable("Something went wrong")
}
}
}
}
PERSON_LOG_TAG 给出 "expected the BEGIN_ARRAY but was BEGIN_OBJECT error"。知道如何解决吗?这通常是通过创建一个外部 JSON class 来解决的,比如 PersonResponse 并且它有一个真实的列表 class。但是我不知道如何使用另一个对象中的列表来完成它。
如果 API 按约定创建,而不是 persons/{personId}
应该 return Person 对象而不是 Persons 列表。所以我假设你应该更换
@GET("persons/{personId}")
fun getPerson(@Path("personId") id: Int): Observable<List<Person>>
到
@GET("persons/{personId}")
fun getPerson(@Path("personId") id: Int): Observable<Person>