Atitit json array to csv
目录
1. Lib
1.1. Apache csv lib
1.2. Org.json.jar cdl模式
CSV | Component for reading and writing comma separated value files |
2. other
2.1. Ret fmt
VCF08,VCF09,ACF01,VCF54,VCF57,VCF10,VCF11,VCF32,VAP01,VCF70,VCF18,VCF01,BCE03A,BCE01A,VCF68,VCF67,VAA07,VCF66,VCF65,VCF69,VCF86,VAA01,VCF23,VCF22,VCF27
1,1526378400000,2,0,0,1526367850870,01,0,0,,1,54501,系统管理员,244,,,55975,22,11,,0,82767,0,1,0
1,1526356800000,2,0,0,1526367777260,01,0,0,,1,54500,系统管理员,244,,,55975,22,11,,0,82767,0,1,0
0,-2209017600000,2,0,0,1526355967273,,0,0,,0,54499,,0,,,0,,,,0,0,0,1,0
1,1526364000000,2,0,0,1526355958717,01,0,0,11111,1,54498,系统管理员,244,1111,1111,55975,111,11,11111,0,82767,0,1,0
1,1526356800000,2,0,0,1526355833170,01,0,0,,1,54497,系统管理员,244,33,33,55975,22,11,,0,82767,0,1,0
1,1526356800000,2,0,0,1526355726100,00,0,0,,1,54495,系统管理员,244,,,55975,,,,0,82767,0,1,0
1,1526356800000,2,,,1526355726100,00,,0,,1,54496,系统管理员,244,,,55975,,,,0,82767,1,1,
2.2. code
package hislog;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.json.CDL;
import org.json.JSONException;
import com.alibaba.fastjson.JSON;
import com.attilax.util.ExUtil;
import com.attilax.util.jsonuti4fc;
public class weijonrecsaveReztloolk {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
*
* @return 返回csv格式的字符串
* @throws JSONException
*/
public String getCSVString(String jsonString) {
// 将jsonArray转换成纯字符串(涵盖所有符号)
// 利用字符串生成org.json.JSONArray,实现net.sf.json.jsonArray与org.json.JSONArray转换
org.json.JSONArray orgjsonarray;
try {
orgjsonarray = new org.json.JSONArray(jsonString);
// 利用org.json工具类生成CSV格式要求的String。
String csv = CDL.toString(orgjsonarray);
return csv;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ExUtil.throwExV2(e);
}
return jsonString;
}
2.3. Other net code
* * @类名称:CSVUtils * @类描述:csv文件读写 * @创建时间:2017年1月10日 上午11:05:12 * @version 1.0.0 */ public class CSVUtils { private static final Logger log = Logger.getLogger(CSVUtils.class); // 写csv文件 传参数文件名 路径 csv文件表头 需要写入的数据 public static File writeCsvFile(String fileName, String path, String[] fileHeaders, List<List<String>> list) { File csvFile = null; BufferedWriter csvFileOutputStream = null; CSVPrinter csvPrinter = null; CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(fileHeaders); try { File file = new File(path); if (!file.exists()) { file.mkdir(); } csvFile = new File(path+fileName+".csv"); csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024); // 初始化 CSVPrinter csvPrinter = new CSVPrinter(csvFileOutputStream, csvFileFormat); List<String> ls = null; if (list != null) { for (int i = 0; i < list.size(); i++) { ls = new ArrayList<String>(); ls=list.get(i); for (int j = 0; j < ls.size(); j++) { csvPrinter.print(ls.get(j)); } csvPrinter.println();// 换行 } } } catch (Exception e) { e.printStackTrace(); log.error("csv文件写入异常"); } finally { try { csvFileOutputStream.flush(); csvFileOutputStream.close(); csvPrinter.close(); } catch (IOException e) { e.printStackTrace(); } } return csvFile; } //测试写入功能 public static void main(String[] args) { /* String[] fileHeaders={"1","2","3"}; */ String[] fileHeaders = null; List<List<String>> list = new ArrayList<List<String>>(); List<String> ls = new ArrayList<String>(); ls.add("a"); ls.add("b"); ls.add("c"); List<String> ls1 = new ArrayList<String>(); ls1.add("北京"); ls1.add("上海"); ls1.add("成都"); list.add(ls); list.add(ls1); File file = writeCsvFile("abc", "E:/test",fileHeaders,list); } // 读取csv文件 传参数 文件 表头 从第几行开始 public static List readCsvFile(File file, String[] fileHeaders, Integer num) { BufferedReader br = null; CSVParser csvFileParser = null; List list = null; // 创建CSVFormat(header mapping) CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(fileHeaders); try { // 初始化FileReader object br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));//解决乱码问题 // 初始化 CSVParser object csvFileParser = new CSVParser(br, csvFileFormat); // CSV文件records List<CSVRecord> csvRecords = csvFileParser.getRecords(); List data = new ArrayList(); list = new ArrayList(); for (int i = num; i < csvRecords.size(); i++) { CSVRecord record = csvRecords.get(i); for (int j = 0; j < fileHeaders.length; j++) { data.add(record.get(fileHeaders[j])); } list.add(data); } } catch (Exception e) { e.printStackTrace(); log.error("csv文件读取异常"); } finally { try { br.close(); csvFileParser.close(); } catch (IOException e) { e.printStackTrace(); } } return list; } }
3. Ref
Apache-Commons CSV文件的读和写 - CSDN博客.html
apache commons csv写文件demo-布布扣-bubuko.com.html
JsonArry转换为CSV,Excel可读 - CSDN博客.html
Apache common Csv读写文件 - CSDN博客.html ()jeig implt d bijyao hao