java读取csv文件并将其转成json
来源:互联网 发布:1加到100的c语言程序 编辑:程序博客网 时间:2024/05/29 04:45
public class CsvUtil {private String fileName = null; private BufferedReader br = null; private List<String> list = new ArrayList<String>(); public CsvUtil() { } public CsvUtil(String fileName) throws Exception { this.fileName = fileName; br = new BufferedReader(new FileReader(fileName)); String stemp; while ((stemp = br.readLine()) != null) { list.add(stemp); } } public List getList() { return list; } /** * 获取行数 * @return */ public int getRowNum() { return list.size(); } /** * 获取列数 * @return */ public int getColNum() { if (!list.toString().equals("[]")) { if (list.get(0).toString().contains(",")) {// csv为逗号分隔文件 return list.get(0).toString().split(",").length; } else if (list.get(0).toString().trim().length() != 0) { return 1; } else { return 0; } } else { return 0; } } /** * 获取制定行 * @param index * @return */ public String getRow(int index) { if (this.list.size() != 0) { return (String) list.get(index); } else { return null; } } /** * 获取指定列 * @param index * @return */ public String getCol(int index) { if (this.getColNum() == 0) { return null; } StringBuffer sb = new StringBuffer(); String tmp = null; int colnum = this.getColNum(); if (colnum > 1) { for (Iterator it = list.iterator(); it.hasNext();) { tmp = it.next().toString(); sb = sb.append(tmp.split(",")[index] + ","); } } else { for (Iterator it = list.iterator(); it.hasNext();) { tmp = it.next().toString(); sb = sb.append(tmp + ","); } } String str = new String(sb.toString()); str = str.substring(0, str.length() - 1); return str; } /** * 获取某个单元格 * @param row * @param col * @return */ public String getString(int row, int col) { String temp = null; int colnum = this.getColNum(); if (colnum > 1) { temp = list.get(row).toString().split(",")[col]; } else if(colnum == 1){ temp = list.get(row).toString(); } else { temp = null; } return temp; } public void CsvClose()throws Exception{ this.br.close(); } /** *去表头 **/ public String removehead(String str){String[] str_1=str.split(",");String sb=new String();for(int i=1;i<str_1.length;i++){sb=sb+str_1[i]+",";}return sb; }}
public JSONArray readcsv(String path){JSONArray array=new JSONArray();CsvUtil util;try {util = new CsvUtil(path);int row=util.getRowNum();int col=util.getColNum();for(int i=0;i<col;i++){JSONObject jsonobject=new JSONObject();String value=util.getCol(i);jsonobject.put(util.getString(0, i),util.removehead(value));array.add(jsonobject);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return array;}
将其表头设为key 其他内容设为value
0 0
- java读取csv文件并将其转成json
- java读取csv文件并将其转成json
- Java读取csv文件并将内容插入到数据库
- Java读取csv文件并将内容插入到数据库
- PHP程序,将csv转成sql文件并导入数据库
- java将float数据写入csv 并在python中读取csv文件
- Java读取CSV文件并导出
- 将JSON内容写入并读取文件
- [Unity][JSON][csv][JSONUnity]csv-JSON如何在CSV文件中保存数组并读取JSON数组
- VC++读取文件内容并将其存入二维数组
- 读取xml文件,并将其存入数据库中
- 读取Excel文件中的数据,并将其导入数据库
- 读取已经存在的excel文件并将其导出
- java读取CSV文件
- Java读取CSV文件
- Java 读取 CSV 文件
- java读取csv文件
- java读取csv文件 .
- 原型模式(设计模式_09)
- cannot call methods on tooltip prior to initialization; attempted to call method 'hide'
- InvokeRequired和Invoke
- 工作分配问题
- 水漫金山:OpenCV漫水填充算法(Floodfill)
- java读取csv文件并将其转成json
- 数据结构期末总结
- W77E516机器周期与时钟周期
- OC与JS的互调
- SPARK+ANSJ 中文分词基本操作
- jquery层级选择器
- 第十五周项目1-(1)-验证哈希表实施查找的相关算法
- 【VSCode】Windows下VSCode编译调试c/c++
- 女孩子:现代的“三从四德”