java convert list of object array to a POJO

前端 未结 1 534
挽巷
挽巷 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:

    <?xml version="1.0" encoding="UTF-8"?>
    <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">
        <mapping>
            <class-a>java.lang.Object[]</class-a>
        <class-b>ch.romix.dozertest.Main.PojoObject</class-b>
            <field>
            <a>this[0]</a>
            <b>Integer</b>
        </field>
        <field>
            <a>this[1]</a>
            <b>Longg</b>
        </field>
        <field>
            <a>this[2]</a>
            <b>String</b>
        </field>
        </mapping>
    </mappings>
    

    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)
提交回复
热议问题