简体   繁体   中英

How to deserealize JSON array into HashMap using jackson?

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM