java - CSV 生成器不支持属性的数组值

标签 java csv jackson converters

我正在尝试开发一个将 JSON 转换为 CSV 的应用程序。

我正在使用 JACKSON 解析 JSON 并写入 CSV

这是我正在尝试解析的 JSON示例:

{ "_id" : 0, 
 "name" : "aimee Zank", 
 "scores" : 
           [ 
             { "type" : "exam", "score" :   1.463179736705023 }, 
             { "type" : "quiz", "score" : 11.78273309957772 },  
             { "type" : "homework", "score" : 6.676176060654615 },  
             { "type" : "homework", "score" : 35.8740349954354 } 
            ] 
     }

  { "_id" : 1, 
    "name" : "Aurelia Menendez", 
    "scores" : 
              [ 
                { "type" : "exam", "score" : 60.06045071030959 }, 
                { "type" : "quiz", "score" : 52.79790691903873 }, 
                { "type" : "homework", "score" : 71.76133439165544 }, 
                { "type" : "homework", "score" : 34.85718117893772 } 
             ] 
       }

 { "_id" : 2, 
  "name" : "Corliss Zuk", 
   "scores" : 
             [ 
              { "type" : "exam", "score" : 67.03077096065002 }, 
              { "type" : "quiz", "score" : 6.301851677835235 }, 
              { "type" : "homework", "score" : 20.18160621941858 }, 
              { "type" : "homework", "score" : 66.28344683278382 } 
             ] 
         }

两个 Java 类:

JsonNode 类

public class JsonNode {

private String _id;
private String name;
private Collection<ScoreType> scores;

/**
 * @return the _id
 */
public String get_id() {
    return _id;
}
/**
 * @param _id the _id to set
 */
public void set_id(String _id) {
    this._id = _id;
}
/**
 * @return the name
 */
public String getName() {
    return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}
/**
  * @return the scores
 */
public Collection<ScoreType> getScores() {
    return scores;
}
/**
  * @param scores the scores to set
 */
public void setScores(Collection<ScoreType> scores) {
    this.scores = scores;
 }
 }

ScoreType 类

public class ScoreType {
private String type;
private String score;
/**
 * @return the type
 */
public String getType() {
    return type;
}
/**
 * @param type the type to set
 */
public void setType(String type) {
    this.type = type;
}
/**
 * @return the score
 */
public String getScore() {
    return score;
}
/**
 * @param score the score to set
 */
public void setScore(String score) {
    this.score = score;
}

}

JSON转换为CSV的方法

  ObjectMapper mapper=new ObjectMapper();

    JsonNode jsonNode=mapper.readValue(new File("C:\\...\\...\\...\\test.json"), JsonNode.class);

    CsvMapper csvMapper=new CsvMapper();
    CsvSchema schema=csvMapper.schemaFor(JsonNode.class);

    schema=schema.withColumnSeparator(';');

    ObjectWriter myObjectWriter=csvMapper.writer(schema);

    FileOutputStream tempFileOutputStream=new FileOutputStream(out+"\\jsontocsv.csv");
    BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(tempFileOutputStream);
    OutputStreamWriter writerOutputStream=new OutputStreamWriter(bufferedOutputStream,"UTF-8");

    myObjectWriter.writeValue(writerOutputStream,jsonNode);

控制台输出:

com.fasterxml.jackson.core.JsonGenerationException: CSV generator does not support 

属性的数组值

CSV 输出:

"0";"aimee Zank";

这就是我到目前为止所做的。

所以我在这里面临两个问题:

1) 输出的 CSV 不完整,它只创建一行并且不写入分数。

2)控制台中的错误。

我正在使用这些JACKSON依赖项:

  <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-csv</artifactId>
        <version>2.4.0</version>
    </dependency> 

谁能帮我解决这些问题吗?

我希望我说得足够清楚。

编辑我期待的 CSV:

_id;name;scores.type;scores.score;scores.type;scores.score;scores.type;scores.score
0;aimee Zank;exam;1.46;quiz;11.78;homework;6.67;homework;35.87

伊斯梅尔

最佳答案

由于 CSV 是简单值的元组,因此它确实不支持集合(JSON 数组)作为列值。你有一个Collection<ScoreType>作为你的 bean 的属性之一,这会导致你的错误。

建议:添加String getter 将您的集合转换为字符串,并手动构建 CSV 架构以避免自动使用集合值列。

关于java - CSV 生成器不支持属性的数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24799457/

相关文章:

python - 如何根据多个条件删除csv文件中的一行?

java - Jackson - 在运行时修改属性而无需注释

java - 确保 Swing GUI 与底层数据结构保持同步

java - 为什么 Eclipse 使用 Cp1252 编码?

Java - 读取 CSV 并将对象 [] 值转换为字符串值

java - 为什么使用 webflux 的 spring boot 测试会忽略自定义 jackson 模块

java - ObjectMapper 将日期更改为字符串

java - 如何在不连接数据库的情况下通过Java创建BLOB对象

java - 使用 CSS 样式表在 html 中显示 XML

PhP上传CSV到MySQL与复杂的决斗NOT EXIST