英文:
Converting a JSONArray to csv using java
问题
我有一个类似这样的JSONArray ->
[
{ "Name" : "Test1", "Age" : 10, "Gender" : "M", "Description" : "Hello World" },
{ "Name" : "Test2", "Age" : 21, "Gender" : "M", "Description" : "Bye\nWorld" }
]
我需要将其转换为CSV格式。我之前是使用 org.json.CD
来实现这一点,像这样:
String csv = CDL.toString(arr);
其中 arr
就是上面给出的JSONArray。生成的CSV字符串是:
Name,Age,Gender,Description
Test1,12,M,Hello World
Test2,21,M,Bye
World
但这并不是正确的转换。正确的转换应该是:
Name,Age,Gender,Description
Test1,12,M,Hello World
Test2,21,M,Bye\nWorld
没有办法手动将字符串 Name,Age,Gender,Description\nTest1,12,M,Hello World\nTest2,21,M,Bye\nWorld
更改为类似于 Name,Age,Gender,Description\nTest1,12,M,Hello World\nTest2,21,M,Bye\\nWorld
。我该如何解决这个问题?
附注:JSONArray 中的值(name、age、gender、description)不是固定的。我正在寻找一种以通用方式解决这样的问题。
英文:
I have a JSONArray like this ->
[
{ "Name" : "Test1", "Age" : 10, "Gender" : "M", "Description" : "Hello World" },
{ "Name" : "Test2", "Age" : 21, "Gender" : "M", "Description" : "Bye\nWorld" }
]
I need to convert this into a csv format. I was using org.json.CD
to achieve this like so
String csv = CDL.toString(arr);
where arr
is the JSONArray as given above. The generated csv string is Name,Age,Gender,Description\nTest1,12,M,Hello World\nTest2,21,M,Bye\nWorld
. The csv output of this would be something like this
Name,Age,Gender,Description
Test1,12,M,Hello World
Test2,21,M,Bye
World
But that is not the correct conversion. The correct conversion would have been
Name,Age,Gender,Description
Test1,12,M,Hello World
Test2,21,M,Bye\nWorld
There is no way to manually change this string Name,Age,Gender,Description\nTest1,12,M,Hello World\nTest2,21,M,Bye\nWorld
to something like Name,Age,Gender,Description\nTest1,12,M,Hello World\nTest2,21,M,Bye\\nWorld
How do I solve this problem?
PS The values in the JSONArray(name, age, gender, description) are not fixed. I am looking for a way to solve a problem like so in a generic fashion
答案1
得分: 0
代替手动生成CSV数据,使用CSV解析器(例如OpenCSV)来为您生成。这样,代替:
Test2,21,M,Bye
World
您将获得以下格式:
Test2,21,M,"Bye
World"
或者
"Test2","21","M","Bye
World"
同样的解析器将能够正确地读取回原始数据。
示例:
//写入CSV
StringWriter sw = new StringWriter();//为演示目的创建字符串
CSVWriter csvWriter = new CSVWriter(sw);
String[] array = {"Test2","21","M","Bye\nWorld"};
csvWriter.writeNext(array,false);
String csvString = sw.toString();
System.out.println(csvString);
//读取CSV
CSVReader csvReader = new CSVReader(new StringReader(csvString));
//在多行情况下
for (String[] line : csvReader){//在多行情况下
for (String cell : line){//处理每行
System.out.print("<"+cell + ">");
}
System.out.println();
}
输出:
Test2,21,M,"Bye
World"
<Test2><21><M><Bye
World>
正如您所看到的
"Bye
World"
被正确解释为单个带有换行符字符(\n
)的“cell”,并且能够打印为
<Bye
World>
英文:
Instead of generating CSV data "manually" use CSV parser (like OpenCSV) to generate it for you. This way instead of
Test2,21,M,Bye
World
you will get format like
Test2,21,M,"Bye
World"
or
"Test2","21","M","Bye
World"
which same parser will be able to properly read back to original data.
DEMO:
//writing CSV
StringWriter sw = new StringWriter();//to create String for demonstration purpose
CSVWriter csvWriter = new CSVWriter(sw);
String[] array = {"Test2","21","M","Bye\nWorld"};
csvWriter.writeNext(array,false);
String csvString = sw.toString();
System.out.println(csvString);
//reading CSV
CSVReader csvReader = new CSVReader(new StringReader(csvString));
//in case of many lines
for (String[] line : csvReader){//in case of many lines
for (String cell : line){//handle each line
System.out.print("<"+cell + ">");
}
System.out.println();
}
Output:
Test2,21,M,"Bye
World"
<Test2><21><M><Bye
World>
As you see
"Bye
World"
is properly interpreted as single cell
with line separator character (\n
) and was able to be print as
<Bye
World>
专注分享java语言的经验与见解,让所有开发者获益!
评论