Convert LinkedHashMap to an object in Java

后端 未结 2 1605
你的背包
你的背包 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 01:59

    Reflection is the only way for setting properties of generic unknown object.
    You can find everything you will need in the docs.

    0 讨论(0)
  • 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<String, String> 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

    0 讨论(0)
提交回复
热议问题