使用RestTemplate如何将递增数字映射为对象的键?

时间:2018-04-10 11:12:13

标签: java json jackson

以下是我从REST端点获取的数据示例:

{
  "0":{
    "id":"1",
    "name":"List #1"
  },
  "1":{
    "id":"2",
    "name":"Some other list"
  },
  "2":{
    "id":"3",
    "name":"A third object"
  },
  "3":{
    "id":"6",
    "name":"You get the idea"
  },
  "result_code":1,
  "result_message":"Success: Something is returned",
  "result_output":"json"
}

我正在努力将其映射到Java中的Object。

这样做的愚蠢方式是这样的:

public class MyLists
{
  @JsonProperty("0")
  ActiveCampaignList list0;
  @JsonProperty("1")
  ActiveCampaignList list1;
  @JsonProperty("2")
  ActiveCampaignList list2;
  @JsonProperty("3")
  ActiveCampaignList list3;
  // plus an arbitrary number of additional list# variables

  // getters and setters
}

必须有更好的方法将返回的JSON映射到Java对象吗?

请注意,我正在使用rt.postForEntity()从RESTful端点获取JSON

0 个答案:

没有答案