Java Json数组列表转换为CSV格式
我正在将JSON对象转换为所需格式的CSV转换。我试过多种方法。。但是没有看。任何要点都将不胜感激Java Json数组列表转换为CSV格式,java,arrays,json,csv,arraylist,Java,Arrays,Json,Csv,Arraylist,我正在将JSON对象转换为所需格式的CSV转换。我试过多种方法。。但是没有看。任何要点都将不胜感激 Here is the output I am getting when I get convert JSON object to JSON Array. JSONObject jsonObj = new JSONObject(content.toString()); JSONArray docs = jsonObj.getJSONArray("Results
Here is the output I am getting when I get convert JSON object to JSON Array.
JSONObject jsonObj = new JSONObject(content.toString());
JSONArray docs = jsonObj.getJSONArray("Results");
System.out.println("get the array"+docs);
File file = new File("C:\\MYVIEW.csv");
String csv =CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
输出结果现在在这里
get the array
[
{"Item":1,"Status":"Approved","Name":"Brownie","SubmitDate":"2016-04-05T14:06:16Z","ReceivedState":1},
{"Item":3,"Status":"Approved","Name":"Oats","SubmitDate":"2016-04-19T19:39:25Z","ReceivedState":1},
{"Item":2,"Status":"Submitted","Name":"Choclate","SubmitDate":"2016-06-01T12:40:19Z","ReceivedState":1},
]
现在,我期待在csv以下格式的输出
Item,Status,Name,SubmittedDate,ReceivedState
1,Approved,Brownie,"2016-04-05T14:06:16Z",1
3,Approved,Oats,"2016-04-19T19:39:25Z",1
2,Submitted,Choclate,"016-06-01T12:40:19Z",1
以下代码是例外情况:
线程“main”java.lang.StringIndexOutOfBoundsException中的异常:
字符串索引超出范围:0
我知道,因为我们这里有存储在文档中的ArrayList。我不确定如何进一步转换为预期的CSV格式。如果有人能给我引路,我将不胜感激
Here is the output I am getting when I get convert JSON object to JSON Array.
JSONObject jsonObj = new JSONObject(content.toString());
JSONArray docs = jsonObj.getJSONArray("Results");
System.out.println("get the array"+docs);
File file = new File("C:\\MYVIEW.csv");
String csv =CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
如果需要任何细节,请告诉我。谢谢请查看代码:
public static void main(String[] args) throws IOException {
String inputJson = "{Results: [\n" +
" {\"Item\":1,\"Status\":\"Approved\",\"Name\":\"Brownie\",\"SubmitDate\":\"2016-04-05T14:06:16Z\",\"ReceivedState\":1},\n" +
" {\"Item\":3,\"Status\":\"Approved\",\"Name\":\"Oats\",\"SubmitDate\":\"2016-04-19T19:39:25Z\",\"ReceivedState\":1},\n" +
" {\"Item\":2,\"Status\":\"Submitted\",\"Name\":\"Choclate\",\"SubmitDate\":\"2016-06-01T12:40:19Z\",\"ReceivedState\":1},\n" +
" ]}";
JSONObject jsonObj = new JSONObject(inputJson);
JSONArray objJsonArray = jsonObj.getJSONArray("Results");
String csv = CDL.toString(objJsonArray);
System.out.println(csv);
FileUtils.writeStringToFile(new File(System.getenv("HOME")+ "/temp.csv"), csv);
}
输出:
Status,Item,SubmitDate,ReceivedState,Name
Approved,1,2016-04-05T14:06:16Z,1,Brownie
Approved,3,2016-04-19T19:39:25Z,1,Oats
Submitted,2,2016-06-01T12:40:19Z,1,Choclate
请看一下代码:
public static void main(String[] args) throws IOException {
String inputJson = "{Results: [\n" +
" {\"Item\":1,\"Status\":\"Approved\",\"Name\":\"Brownie\",\"SubmitDate\":\"2016-04-05T14:06:16Z\",\"ReceivedState\":1},\n" +
" {\"Item\":3,\"Status\":\"Approved\",\"Name\":\"Oats\",\"SubmitDate\":\"2016-04-19T19:39:25Z\",\"ReceivedState\":1},\n" +
" {\"Item\":2,\"Status\":\"Submitted\",\"Name\":\"Choclate\",\"SubmitDate\":\"2016-06-01T12:40:19Z\",\"ReceivedState\":1},\n" +
" ]}";
JSONObject jsonObj = new JSONObject(inputJson);
JSONArray objJsonArray = jsonObj.getJSONArray("Results");
String csv = CDL.toString(objJsonArray);
System.out.println(csv);
FileUtils.writeStringToFile(new File(System.getenv("HOME")+ "/temp.csv"), csv);
}
输出:
Status,Item,SubmitDate,ReceivedState,Name
Approved,1,2016-04-05T14:06:16Z,1,Brownie
Approved,3,2016-04-19T19:39:25Z,1,Oats
Submitted,2,2016-06-01T12:40:19Z,1,Choclate
什么是CDL?或者?这是什么CDL?还是这个