【问题标题】:CSV generator does not support Array values for propertiesCSV 生成器不支持属性的数组值
【发布时间】:2014-09-08 02:00:39
【问题描述】:

我正在尝试开发一个将 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;
}

}

JSONCSV

的方法
  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 中如何?我的意思是 csv 中的一行应该是什么样子
  • 您正在混合 1.x 和 2.x Jackson 版本。坚持 2.x。
  • @AlexeyGavrilov 你能看看我的编辑吗?
  • @IsmailSen 你有错误的库依赖。参考这个页面:github.com/FasterXML/jackson-core
  • @AlexeyGavrilov 我已经更正了 maven depandecies。我只用com.fasterxml.jackson.core

标签: java csv jackson converter


【解决方案1】:

由于 CSV 是简单值的元组,它确实不支持将集合(JSON 数组)作为列值。您有一个 Collection&lt;ScoreType&gt; 作为 bean 的属性之一,这导致了您的错误。

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

【讨论】:

  • 能否请您详细说明您的建议
  • 虽然 CSV 确实没有原生数组/列表类型,但 Jackson 确实允许对逻辑数组使用“单元内”分隔符。默认分隔符是分号(字符;),只要您的属性类型是Java数组或Collection,一切都应该适用于足够新的Jackson CSV模块版本(我认为至少需要2.5)。
【解决方案2】:

尝试在 JsonNode 类中为 Collection 属性使用自定义 json 序列化器,并将列表转换为字符串。

public class JsonNode {

private String _id;
private String name;
@JsonSerialize(using=CollectionSerializer.class)
private Collection<ScoreType> scores;

}


public class CollectionSerializer extends StdSerializer<Collection<Object>> {
    private static final long serialVersionUID = 1L;

    protected CollectionSerializer(Class<Collection<Object>> t) {
        super(t);
    }

    public CollectionSerializer() {
        this(null);
    }

    @Override
    public void serialize(Collection<Object> value, JsonGenerator gen, SerializerProvider provider) throws IOException {

        gen.writeString(value.stream().map(String::valueOf).collect(Collectors.joining(",")).toString());

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2010-10-10
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多