使用 Retrofit 解析键值对

huangapple go评论60阅读模式
英文:

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&lt;String, String&gt;. The models could look like (assuming you're using Gson. Should be similar for others):

data class ApiResponse(
  @SerializedName(&quot;result&quot;)
  val result: String,
  @SerializedName(&quot;responseCode&quot;)
  val responseCode: Int,
  @SerializedName(&quot;message&quot;)
  val message: String,
  @SerializedName(&quot;data&quot;)
  val data: List&lt;Map&lt;String, String&gt;&gt;
)

huangapple
  • 本文由 发表于 2020年9月11日 14:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定