0

Here's a sample of the data I get from my REST endpoint:

{
  "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"
}

I'm struggling to map this to an Object in Java.

The stupid way of doing this that works is like this:

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
}

There must be a better way of mapping the returned JSON to a Java object right?

Note that I'm using rt.postForEntity() to get JSON from the RESTful endpoint

Trevor
  • 1,137
  • 1
  • 19
  • 33
  • try using Map – Avinash Anand Apr 10 '18 at 11:27
  • I tried using `Map list = new HashMap<>();` but the issue is that it won't match it because of the variable name of the Map, it needs to match "0", "1", "2", "3" etc. – Trevor Apr 10 '18 at 11:32
  • you may have to write some custom deserializer. – Avinash Anand Apr 10 '18 at 12:03
  • 1
    @AvinashAnand you're right, I used this article to help me solve my problem using your suggestion of a custom deserializer: https://stackoverflow.com/questions/11376304/right-way-to-write-json-deserializer-in-spring-or-extend-it – Trevor Apr 10 '18 at 13:23
  • Please post your answer so others can benefit if they happen to come across similar problem. – Avinash Anand Apr 11 '18 at 04:58

0 Answers0