I have json like this:
{ "users":{ "1234":{ "firstname":"Joe", "lastname":"Smith" }, "9876":{ "firstname":"Bob", "lastname":"Anderson" } }, "jobs":[ { "id":"abc", "location":"store" }, { "id":"def", "location":"factory" } ] }
I parse this with Jackson, so I parsed the answers using: readvalue (json, MyCustomClass.class)
Where MyCustomClass looks like
public class MyCustomClass{ @JsonProperty("jobs") ArrayList<Job> jobs; @JsonProperty("users") ArrayList<UserMap> usersMap; }
Now the tasks are well versed in Jobs' objects, but I can not get users to understand, since they have dynamic keys. I read about JsonAnyGetter / Setter and tried to create a UserMap object map that displays a line -> User like:
public class UserMap { private HashMap<String,User> usersMap; @JsonAnySetter public void add(String key, User user){ usersMap.put(key, user); } @JsonAnyGetter public Map<String, User> getUsersMap(){ return usersMap; } }
but that will not work. I think I can do this with the TypeReference shell, but I can only think about how to do this if these cards were the only type I returned. Since I get different types back (users and jobs), can this be done?
source share