java convert list of object array to a POJO

前端 未结 1 537
挽巷
挽巷 2021-01-16 04:31

How to convert

List to List

Here is example

//So, lets us say I have Object[], I want to         


        
1条回答
  •  悲哀的现实
    2021-01-16 04:51

    Maybe http://dozer.sourceforge.net can help you. It is a mapping library configurable by xml.

    I tried it shortly with this:

    public class Main {
      public static void main(String[] args) {
        Object[] obj = new Object[3];
        obj[0] = new Integer(10);
        obj[1] = new Long(2346246234634L);
        obj[2] = "Hello";
    
        Collections.singletonList("mapping.xml");
        DozerBeanMapper mapper = new DozerBeanMapper(Collections.singletonList("mapping.xml"));
        PojoObject pojo = mapper.map(obj, PojoObject.class);
        System.out.println(pojo);
      }
    
      public static class PojoObject {
        private Integer integer;
        private Long longg;
        private String string;
    
        public PojoObject() {}
    
        public Integer getInteger() {
          return integer;
        }
    
        public void setInteger(Integer integer) {
          this.integer = integer;
        }
    
        public Long getLongg() {
          return longg;
        }
    
        public void setLongg(Long longg) {
          this.longg = longg;
        }
    
        public String getString() {
          return string;
        }
    
        public void setString(String string) {
          this.string = string;
        }
    
        @Override
        public String toString() {
          return String.format("Pojo content: %d, %d, %s", integer, longg, string);
        }
      }
    }
    

    My mappings.xml looks like this:

    
    
        
            java.lang.Object[]
        ch.romix.dozertest.Main.PojoObject
            
            this[0]
            Integer
        
        
            this[1]
            Longg
        
        
            this[2]
            String
        
        
    
    

    Unfortunately it only mapped 10 to all three PojoObject properties. Maybe you can see the error and use the snippet for your code. Maybe it is a bug in Dozer... I couldn't find any example using this[0].

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