英文:
Parse key value pair using retrofit
问题
{
"result": "success",
"response_code": 200,
"message": "data list",
"data": [
{
"Assigned": "14",
"Closed": "150",
"Escalated TTO": "102",
"Escalated TTR": "2",
"New": "44",
"Pending": "4",
"Resolved": "14"
}
]
}
{
"result": "success",
"response_code": 200,
"message": "data list",
"data": [
{
"Assigned": "14",
"Closed": "150",
"Escalated TTO": "102",
"Escalated TTR": "2",
"New": "44",
"Pending": "4",
"Resolved": "14",
"xx": "xx",
"xx": "xx",
"xx": "xx"
}
]
}
英文:
{
"result": "success",
"response_code": 200,
"message": "data list",
"data": [
{
"Assigned": "14",
"Closed": "150",
"Escalated TTO": "102",
"Escalated TTR": "2",
"New": "44",
"Pending": "4",
"Resolved": "14"
}
]
}
Above is the response that I want to parse, but I don't want to create a static POJO for it. The model should be dynamic so that even if there is another key-value pair in the above response, like as shown below, the response is able to be parsed I guess it is possible using HashMap
.
{
"result": "success",
"response_code": 200,
"message": "data list",
"data": [
{
"Assigned": "14",
"Closed": "150",
"Escalated TTO": "102",
"Escalated TTR": "2",
"New": "44",
"Pending": "4",
"Resolved": "14",
"xx": "xx",
"xx": "xx",
"xx": "xx",
}
]
}
答案1
得分: 2
按照评论中的讨论,理想情况应该是一个 Map<String, String>
。模型可能如下所示(假设您在使用 Gson,其他类似的库也应该类似):
data class ApiResponse(
@SerializedName("result")
val result: String,
@SerializedName("responseCode")
val responseCode: Int,
@SerializedName("message")
val message: String,
@SerializedName("data")
val data: List<Map<String, String>>
)
英文:
As discussed in the comments, the ideal scenario would be a Map<String, String>
. The models could look like (assuming you're using Gson. Should be similar for others):
data class ApiResponse(
@SerializedName("result")
val result: String,
@SerializedName("responseCode")
val responseCode: Int,
@SerializedName("message")
val message: String,
@SerializedName("data")
val data: List<Map<String, String>>
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论