Convert LinkedHashMap to an object in Java

后端 未结 2 1604
你的背包
你的背包 2021-01-14 01:26

I\'ll start directly with my question and later on I give more background information.

Simple: I hava a LinkedHashMap

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 02:08

    You can create two classes

    @XmlAccessorType(XmlAccessType.FIELD) 
    public class Document {
        @XmlElement
        private String title;
        @XmlElement
        private String id;
        @XmlElement
        private int version;
    }  
    
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class MapJson {
        @XmlElement
        private LinkedHashMap documents; 
    }  
    

    and cobvert Object to JSON usingg
    Jackson

    new org.codehaus.jackson.map.ObjectMapper().writeValueAsString(instanceofMapJson);  
    

    Google JSON

    new com.google.gson.Gson().toJson(instanceofMapJson);  
    

    PS. Using google json, you can remove xml annotations from your classes

提交回复
热议问题