有没有人能告诉我为什么这个不起作用
模型类:
@JsonClass(generateAdapter = true)
data class InstagramBusinessAccountResponse(
val data : List<Account>
) {
data class Account(
@Json(name = "id") val id : String,
@Json(name = "instagram_business_account") val instagramBusinessAccount : InstagramBusinessAccount
) {
data class InstagramBusinessAccount(
@Json(name = "id") val id: String,
@Json(name = "name") val name: String,
@Json(name = "profile_picture_url") val profilePictureUrl: String = ""
)
}
companion object {
fun fromJson(json: String) : InstagramBusinessAccountResponse {
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(InstagramBusinessAccountResponse::class.java)
return jsonAdapter.fromJson(json)!!
}
}
}在解析下面的json时
{"data":[{"instagram_business_account":{"id":"id","username":"name","name":"Suyash Chavan","profile_picture_url":"image"},"id":"id"}]}使用
InstagramBusinessAccountResponse.fromJson(json.toString())
...
companion object {
fun fromJson(json: String) : InstagramBusinessAccountResponse {
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(InstagramBusinessAccountResponse::class.java)
return jsonAdapter.fromJson(json)!!
}
}给instagramBusinessAccount空值,但是如果我不使用@Json的自定义字段名,比如用instagram_business_account替换instagramBusinessAccount,用profile_picture_url替换profilePictureUrl,它就能正常工作。
发布于 2019-02-27 16:16:00
我在Moshi Builder中缺少.add(KotlinJsonAdapterFactory())。
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()它现在起作用了。
https://stackoverflow.com/questions/54900155
复制相似问题