如何将包含json值的对象写入java中的csv文件?

g6baxovj  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(299)

我有一个对象mydata,它有100个json记录,比如:

[Record{Purpose='Medical ', Date='2020-10-14', ID='9215524400  ', UpdateTime='2020-10-14', Checkin='2020-10-14'},...]

我需要把它写入一个正确的csv格式的csv文件。
这是我的密码:

FileWriter csvWriter = new FileWriter("myFilecsv.csv");
   csvWriter.write(String.valueOf(myData)); //Its writing the file as a String

我尝试使用以下代码转换它:

JFlat flatMe = new JFlat(str);
        //To get the 2D representation
        flatMe.json2Sheet().headerSeparator("_").getJsonAsSheet();
        //write the 2D representation in csv format
        flatMe.write2csv("myFilecsv.csv");

这是一个抛出错误:

java.lang.ClassNotFoundException: com.jayway.jsonpath.Configuration$Defaults

我正在使用以下依赖项

<dependency>
        <groupId>com.github.opendevl</groupId>
        <artifactId>json2flat</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>

有没有更好的方法来实现这一点?请告诉我:

44u64gxh

44u64gxh1#

添加jarjson2flat-1.0.3.jar解决了这个问题。

相关问题