3

I have the JSON object like this:

 "stream_server":{  
         "value":"11",
         "list":[  
            {  
               "id":"11",
               "desc":"EU West"
            },
            {  
               "id":"4",
               "desc":"EU Sud + GB"
            },
            {  
               "id":"9",
               "desc":"DE 1"
            },
            {  
               "id":"12",
               "desc":"DE 2"
            }
         ]
      }

I generated code for Jackson library where "list" is presented as ArrayList of Objects.

public class StreamServer {
    @JsonProperty("value")
    private String value;
    @JsonProperty("list")
    private java.util.HashMap<String, String> serverList = new HashMap<>();
}

Can I deserialize it into Java Object like above?

I am looking for the sample code.

Yuri
  • 1,748
  • 2
  • 23
  • 26
Sartre
  • 143
  • 1
  • 2
  • 13
  • possible duplicate of [Deserializing into a HashMap of custom objects with jackson](http://stackoverflow.com/questions/18002132/deserializing-into-a-hashmap-of-custom-objects-with-jackson) – Bacteria Jul 11 '15 at 19:17
  • @GoodBadandUgly , Thanks for the link. Maybe It will help me. But It is not absolutely my case. For example, value "11" of "id" is the key and value "EU West" of "desc" is the value of my HashMap. In your example "id" is the key, "11" is value and "desc" is key "EU West" is value and so on. Hope, it sounds not too complicated – Sartre Jul 11 '15 at 19:44
  • I've found answer here: http://stackoverflow.com/questions/20105723/using-jackson-to-deserialize-into-a-map?rq=1 – Sartre Jul 11 '15 at 19:49
  • Minor detail, but your JSON is invalid. Either wrap it with curly bracket or remove the stream_server part. – habsq Jul 11 '15 at 20:14
  • @habsq sorry, it was a part of complex object – Sartre Jul 11 '15 at 20:44

1 Answers1

4

You can deserialize it into.

public static  class StreamServer {
    @JsonProperty("value")
    private String value;

    @JsonProperty("list")
    private List<Server> serverList;

}

public static class Server {
    @JsonProperty("id")
    private String id;

    @JsonProperty("desc")
    private String desc;
}

Jackson code to read would be something like below:

    ObjectMapper m  = new ObjectMapper();
    StreamServer s = m.readValue(json, StreamServer.class);
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • I see, but List<> is redundant. – Sartre Jul 11 '15 at 19:28
  • 1
    In your JSON structure, list is a list, not a Map, and each member of list is a Map (or a json object - depends on how you look at it). – Wand Maker Jul 11 '15 at 19:31
  • So, I can't use value of "id" as key for HashMap and value of "desc" as value for HashMap? – Sartre Jul 11 '15 at 19:47
  • 1
    Updated the answer - if you can represent servers using another class instead of Map, then, things will be simpler – Wand Maker Jul 11 '15 at 20:05
  • Thanks, I already did it with automatic tool. And also there is the same question as mine: http://stackoverflow.com/questions/20105723/using-jackson-to-deserialize-into-a-map?rq=1 – Sartre Jul 11 '15 at 20:41
  • @Sartre there is no automated support for all kinds of potential transformations. Fundamentally JSON Array matches Java arrays and `Collection`, and JSON Objects `Map`s and `POJO`s. If you want something non-standard and structurally different, you need to add converters. I am guessing that the tool you are using maybe designed for XML, since those tools are typically problematic with JSON, reason being that as XML does not have distinction between Objects and Arrays (it only has Elements to contain other Elements and text), tools model things in a way that does not work well with JSON. – StaxMan Jul 13 '15 at 21:50
  • There'sa better way in https://www.mkyong.com/java/jackson-convert-json-array-string-to-list/ – Daniel Carpio Contreras Aug 12 '19 at 22:50