[英]How to convert json Array to CSV file using Java
我一直在尋找如何將我的 json 數組轉換為 CSV 文件,但無法得到明確的答案。 如果有人做過,請幫助我如何轉換它或分享任何有用的文檔,謝謝。
庫鏈接: https : //github.com/opendevl/Json2Flat
示例輸出如下: 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
你可以使用傑克遜。
依賴項:
<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>
代碼示例:
@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;
}
}
現在消費者:
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輸出:
[ {
"firstName": "約翰",
"lastName" : "狼",
“年齡”:26
]]
CSV輸出:
名字,姓氏,年齡
約翰,沃爾夫,26
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.