使用super-CSV读取嵌套的POJO

时间:2016-07-14 07:00:22

标签: java dozer supercsv

我试图用CSV加载嵌套的POJO。对于某些错误,我无法使其正常工作。

请在下面找到我的代码。以下类Person包含两个POJO字段地址和attribs

AmCharts.addInitHandler(function(chart) {
  // iterate through data
  for (var i = 0; i < chart.dataProvider.length; i++) {
    var dp = chart.dataProvider[i];

    dp.total = 0;
    dp.totalText = 0;
    dp.totalSymbol="$"; //added currency here
    for (var x = 0; x < chart.graphs.length; x++) {
      var g = chart.graphs[x];
      dp.totalText += dp[g.valueField];
      if (dp[g.valueField] > 0)
        dp.total += dp[g.valueField];
      if (dp.total == 0) {
           dp.total = "";dp.totalText="";dp.totalSymbol=""; //empty everything
      };
    }
  }
  var graph = new AmCharts.AmGraph();
  graph.valueField = "total";
  graph.labelText = "[[totalSymbol]] [[totalText]]";
  graph.visibleInLegend = false;
  graph.lineAlpha = 0;
  graph.labelPosition="right";//set label to extreme right
  graph.showBalloon = false;
  graph.fontSize = 13;
  chart.addGraph(graph);
}, ["serial"]);

地址类

public class Person
{
    public String          firstname;
    public String          lastname;
    public PersonAttribute attribs;
    public Address         address;
    //Getters and setters
}


PersonAttribute class 

{
   public int height;
   public int weight;
   //Getters and setters
}

//吸气者和二传手     }

GeoLocation中

public class Address
{
   public String      city;
   public String      zipCode;
   public GeoLocation geoLocation;

//吸气者和二传手     }

现在我正在尝试阅读CSV并填充人员课程。我无法使它发挥作用。这是我的代码

public class GeoLocation
{
    private String latitude;
    private String longitude;

我收到以下错误     线程“main”中的异常org.dozer.MappingException:没有为类中的字段(高度)找到读取或写入方法

1 个答案:

答案 0 :(得分:1)

我得到了它的工作。问题在于字段映射。

 private static void readWithCsvDozerBeanReader (final Reader reader) throws IOException
{
    ICsvDozerBeanReader beanReader = null;
    try
    {
        beanReader = new CsvDozerBeanReader(reader, PREFS);

        final String[] header = beanReader.getHeader(true);

        // set up the field mapping, processors and hints dynamically
        final String[] fieldMapping = new String[header.length];
        final CellProcessor[] processors = new CellProcessor[header.length];
        final Class<?>[] hintTypes = new Class<?>[header.length];
        for (int i = 0; i < header.length; i++)
        {
            fieldMapping[i] = header[i];
            if (header[i].equals("firstname"))
            {
                processors[i] = new NotNull();
            }
            if (header[i].equals("height") || header[i].equals("weight"))
            {
                processors[i] = new ParseInt();
            }
        }

        beanReader.configureBeanMapping(Person.class, fieldMapping, hintTypes);

        Person person;
        while ((person = beanReader.read(Person.class, processors)) != null)
        {
            System.out.println(String.format("lineNo=%s, rowNo=%s, person=%s", beanReader.getLineNumber(),
                    beanReader.getRowNumber(), person));
        }

    }
    finally
    {
        if (beanReader != null)
        {
            beanReader.close();
        }
    }
}