CSV生成器不支持属性的数组值 [英] CSV generator does not support Array values for properties

查看:1031
本文介绍了CSV生成器不支持属性的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



JACKSON 解析 JSON 并写入 CSV




$ b <$ p> {_id :0,
name:aimee Zank,
scores:
[
{type:exam,score:1.463179736705023} b $ b {type:quiz,score:11.78273309957772},
{type:homework,score:6.676176060654615},
{type: ,score:35.8740349954354}
]
}

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

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

这两个Java类:



JsonNode类

  public class JsonNode {

private String _id;
private String name;
private Collection< ScoreType>分数;

/ **
* @返回_id
* /
public String get_id(){
return _id;
}
/ **
* @param _id _id设置
* /
public void set_id(String _id){
this._id = _ID;
}
/ **
* @返回名称
* /
public String getName(){
return name;
}
/ **
* @param命名要设置的名称
* /
public void setName(String name){
this.name =名称;
}
/ **
* @返回分数
* /
public Collection< ScoreType> getScores(){
return scores;
}
/ **
* @param对得分设置
* /
public void setScores(Collection< ScoreType> scores){
this .scores = score;
}
}

ScoreType类

  public class ScoreType {
private String type;
private String score;
/ **
* @返回类型
* /
public String getType(){
return type;
}
/ **
* @param键入要设置的类型
* /
public void setType(String type){
this.type =类型;
}
/ **
* @返回分数
* /
public String getScore(){
return score;
}
/ **
* @param得分设置
* /
public void setScore(String score){
this.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生成器不支持

属性的数组值



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;家庭作业; 6.67;家庭作业; 35.87

Ismail

解决方案

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



添加 String getter,将您的集合转换为字符串,并手动构建CSV模式,以避免自动使用Collection-值列。 p>

I'm trying to develop an application that convert JSON to CSV.

I'm using JACKSON to parse JSON and write CSV.

Here is an example of a JSON I'm trying to parse :

{ "_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 } 
             ] 
         }

The two Java Classes :

JsonNode class

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 class

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;
}

}

The method that convert JSON to 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);

The console output :

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

Array values for properties

The CSV output :

"0";"aimee Zank";

That's what I have done so far.

So I'm facing two issues here :

1) The output CSV is not complete, it creates only one line and don't write scores.

2) The error in the console.

I'm using these JACKSON depandencies :

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

Can anyone help me solving these issues ?

I hope I was clear enough.

Edits CSV I'm expecting :

_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

Ismail

解决方案

Since a CSV is a tuple of simple values, it does indeed not support collections (JSON arrays) as column values. You have a Collection<ScoreType> as one of your bean's properties and this is causing your error.

Suggestion: add a String getter which turns your collection into a string, and build a CSV schema manually to avoid automatically using the Collection-valued column.

这篇关于CSV生成器不支持属性的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆