简体   繁体   English

如何使用 Java 将 json 数组转换为 CSV 文件

[英]How to convert json Array to CSV file using Java

I have been searching how to convert my json array to CSV file but could not get clear answer.我一直在寻找如何将我的 json 数组转换为 CSV 文件,但无法得到明确的答案。 If anyone have done it could you please help me how to convert it or just share the like of any useful documentation, Thank you.如果有人做过,请帮助我如何转换它或分享任何有用的文档,谢谢。

Library Link : https://github.com/opendevl/Json2Flat库链接: https : //github.com/opendevl/Json2Flat

Sample Output Like This : https://j2flateval.herokuapp.com示例输出如下: https : //j2flateval.herokuapp.com

 // There are some typos in the data.

// You can try json2flat for converting JSON docs to get an equivalent CSV representation.
// If you want to try for more JSON doc click here.

// For the JSON data :

{
    "results": [{

            "geo_position": {
                "Field1": 11,
                "Field2": 12
            },
            "Field3": 13,
            "Field4": 14,
            "Field5": 15
        },

        {
            "geo_position": {
                "Field1": 21,
                "Field2": 22
            },
            "Field3": 23,
            "Field4": 24,
            "Field5": 25
        }
    ]
}

// The code is also preety simple.

JFlat flatMe = new JFlat(jsonString);
flatMe
    .json2Sheet()
    .headerSeparator("/")
    .write2csv("test.csv");

// This will write the result to test.csv file.

// Equivalent CSV representation :


results/Field3,results/Field4,results/Field5,results/geo_position/Field1,results/geo_position/Field2
    13.0,14.0,15.0,11.0,12.0
    23.0,24.0,25.0,21.0,22.0

You can use Jackson.你可以使用杰克逊。

The dependencies:依赖项:

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

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
</dependency>

A Code example:代码示例:

@JsonPropertyOrder({"firstName", "lastName", "age"})
public class Person {
    private String firstName; 
    private String lastName;
    private Integer age;

    public Person()
    {}

    public Person(String firstName, String lastName, Integer age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

Now the consumer:现在消费者:

public class Consumer {
    private ObjectMapper mapper; 

    private CsvMapper csvMapper; 


    public static void main(String[] args) {
        csvMapper = new CsvMapper();
        mapper = new ObjectMapper();

        List<Person> list = new ArrayList<>();
        simpleList.add(new Person("John", "Wolf", 26));

        Consumer c = new Consumer();

        String json = c.createJson(list);
        System.out.println("The Json file:" + json);

        String csvStr = c.createCsv(list);
        System.out.println("The Csv file:" + csvStr);        
    }


    private String createJson(List<Person> list) throws JsonProcessingException { 
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
    }

    private String createCsvList<Person> list) throws JsonProcessingException {
        CsvSchema schema = csvMapper.schemaFor(Person.class).withHeader();
        return csvMapper.writer(schema).writeValueAsString(list);
    }
}

Json output: JSON输出:

[ { [ {
"firstName" : "John", "firstName": "约翰",
"lastName" : "Wolf", "lastName" : "狼",
"age" : 26 “年龄”:26
} ] ]]

Csv output: CSV输出:

firstName,lastName,age名字,姓氏,年龄
John,Wolf,26约翰,沃尔夫,26

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM