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
做猪小侠源码的代理,提供一站式服务
如果你不懂得搭建网站或者服务器,小程序,源码之类的怎么办? 第一通过本站学习各种互联网的技术 第二就是联系客服,我帮帮你搭建(当然要收取部分的费用) 第三成为我们的代理,我们提供整套的服务。