您好,欢迎访问一九零五行业门户网

如何在Java中将JSON数组转换为CSV?

json可以用作数据交换格式,它是轻量级的且与语言无关。一个jsonarray可以解析文本字符串以生成类似于向量的对象,并支持java.util.list接口。我们可以使用org.json.cdl类将json数组转换为csv格式,它提供了一个静态方法tostring(),用于将jsonarray转换为逗号分隔的文本。我们需要导入org.apache.commons.io.fileutils包,以使用writestringtofile()方法将数据存储在csv文件中。
语法public static java.lang.string tostring(jsonarray ja) throws jsonexception
in the below example, we can convert a json array to csv format.
exampleimport 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.csvlast name,first name,locationchandra,ravi,bangalore
以上就是如何在java中将json数组转换为csv?的详细内容。
其它类似信息

推荐信息