语法
public static java.lang.String toString(JSONArray ja) throws JSONException
登录后复制
In the below example, we can convert a JSON Array to CSV format.
Example
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.*;
public class ConvertJsonToCSVTest {
public static void main(String[] args) throws JSONException {
String jsonArrayString = "{"fileName": [{"first name": "Ravi","last name": "Chandra","location": "Bangalore"}]}";
JSONObject output;
try {
output = new JSONObject(jsonArrayString);
JSONArray docs = output.getJSONArray("fileName");
File file = new File("EmpDetails.csv");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
System.out.println("Data has been Sucessfully Writeen to "+ file);
System.out.println(csv);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
登录后复制
输出
Data has been Sucessfully Writeen to EmpDetails.csv
last name,first name,location
Chandra,Ravi,Bangalore
登录后复制
以上就是如何在Java中将JSON数组转换为CSV?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!