无法使用改造将json数据映射到pojo类

当我查看成功的API响应时,似乎它正在发送数据,但数据无法加载到其对应的Pojo类中,其中ArrayList的大小似乎为空。从我的角度来看,POJO类的结构似乎很好,但无法弄清楚问题出在哪里。在这里,我提供了我的日志屏幕截图,该屏幕截图返回了API数据,并保留了日志以查看ArrayList大小:

My Log screenshot

API响应:

{
  "achievementList": [
    {
        "id": "somerandomuuid2",
        "name": "Foodie",
        "url": "https://toppng.com/uploads/preview/achievement-icon-icon-11553495882s4jdqrtwe2.png",
        "rewardPoint": 50,
        "description": "Order 500 Food items.",
        "earnPoint": 500,
        "userPoint": 450,
        "achieved": false
    },
    {
        "id": "somerandomuuid3",
        "name": "Explorer",
        "url": "https://toppng.com/uploads/preview/achievement-icon-icon-11553495882s4jdqrtwe2.png",
        "rewardPoint": 50,
        "description": "Book more than 100 tickets.",
        "earnPoint": 100,
        "userPoint": 0,
        "achieved": false
    }
],
   "totalRewardPoints": 0
}

成就(POJO班)

data class Achievements(
@Json(name = "achievementList")
var achievementsList: ArrayList<AchievementsList>
)

data class AchievementsList(
@Json(name = "id")
var id: String?,

@Json(name = "name")
var name: String?,

@Json(name = "url")
var url: String?,

@Json(name = "rewardPoint")
var rewardPoint: Int?,

@Json(name = "description")
var description: String?,

@Json(name = "earnPoint")
var earnPoint: Int?,

@Json(name = "userPoint")
var userPoint: Int?,

@Json(name = "achieved")
var achieved: Boolean?
)

API服务

fun getUserAchievements(
context: AppCompatActivity,
userId: String,
listener: OnAchievementsListener
) {
APIClient.normalRequest.getUserAchievements(userId)
    .enqueue(object : Callback<Achievements> {
        override fun onFailure(call: Call<Achievements>, t: Throwable) {
            listener.onFailure(ResponseCodes.badRequest, t.localizedMessage!!)
        }

        override fun onResponse(
            call: Call<Achievements>,
            response: Response<Achievements>
        ) {
            when (response.code()) {
                ResponseCodes.success -> {
                    Log.i("ProfileActivity: ", "Response: ${response.body()}")
                    response.body()?.let { listener.onSuccess(it) }
                }
                else -> {
                    when (response.code()) {
                    listener.onFailure(response.code(), "Something went wrong")

                    }
                }
            }
        }
    })
}

活动

private fun getAchievementsList() {
    appPreferences?.getString(AppPreferences.UUID)?.let {
        getUserAchievements(this, it, object : OnAchievementsListener {
            override fun onFailure(code: Int, description: String) {
                Utils.showToast(this@ProfileActivity, "$code, $description")
            }

            override fun onSuccess(achievements: Achievements?) {
                Log.i(TAG, "Size: ${achievements?.achievementsList?.size}")
            }
        })
    }
}
评论
  • Ula
    Ula 回复

    尝试这个:

    @SerializedName("achievementList")

    代替

    @Json(name = "achievementList")

    在POJO类的所有字段中