java 读取文件并解析json格式数据再存入数据库 java解析json文件 再存入数据库
java\u89e3\u6790json\u683c\u5f0f\u6587\u4ef6\uff0c\u518d\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u600e\u4e48\u505a\uff1fjava\u89e3\u6790json\u683c\u5f0f\u6587\u4ef6\uff0c\u518d\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u7684\u65b9\u6cd5\uff1a1:\u5b9a\u4e49\u4e00\u4e2a\u5b9e\u4f53\u7c7b
2:\u7528json lib\u5c06json\u5b57\u7b26\u4e32\u8f6c\u4e3aJava\u5bf9\u8c61
3:\u7528jdbc\u6216hibernate\u5c06java\u5bf9\u8c61\u5b58\u5165\u6570\u636e\u5e93
\u76f4\u63a5\u8bfb\u5199\u6587\u4ef6\uff0c\u518d\u628a\u8bfb\u51fa\u6765\u7684\u6587\u4ef6\u5185\u5bb9\u683c\u5f0f\u5316\u6210json\uff0c\u518d\u7528JDBC\u3001Mybatis\u6216\u8005\u5176\u4ed6\u6846\u67b6\u5c06json\u6570\u636e\u5b58\u5165\u6570\u636e\u5e93\u3002
\u5047\u8bbe\u5b9e\u4f53\u7c7b\u662f\u8fd9\u6837\u7684\uff1a
public class ElectSet {public String xueqi;public String xuenian;public String startTime;public String endTime;public int menshu;public String isReadDB;//{"xueqi":,"xuenian":,"startTime":,"endTime":,"renshu":,"isReadDB":}public String getXueqi() { return xueqi;}public void setXueqi(String xueqi) { this.xueqi = xueqi;}public String getXuenian() { return xuenian;}public void setXuenian(String xuenian) { this.xuenian = xuenian;}public String getStartTime() { return startTime;}public void setStartTime(String startTime) { this.startTime = startTime;}public String getEndTime() { return endTime;}public void setEndTime(String endTime) { this.endTime = endTime;}public int getMenshu() { return menshu;}public void setMenshu(int menshu) { this.menshu = menshu;}public String getIsReadDB() { return isReadDB;}public void setIsReadDB(String isReadDB) { this.isReadDB = isReadDB;} }\u6709\u4e00\u4e2ajson\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5b58\u7684\u4fe1\u606f\u5982\u4e0b\uff1a
Sets.json:{"xuenian":"2007-2008","xueqi":"1","startTime":"2009-07-19 08:30","endTime":"2009-07-22 18:00","menshu":"10","isReadDB":"Y"}\u5177\u4f53\u64cd\u4f5c\uff1a
/** \u53d6\u51fa\u6587\u4ef6\u5185\u5bb9\uff0c\u586b\u5145\u5bf9\u8c61*/public ElectSet findElectSet(String path){ ElectSet electset=new ElectSet(); String sets=ReadFile(path);//\u83b7\u5f97json\u6587\u4ef6\u7684\u5185\u5bb9 JSONObject jo=JSONObject.fromObject(sets);//\u683c\u5f0f\u5316\u6210json\u5bf9\u8c61 //System.out.println("------------" jo); //String name = jo.getString("xuenian"); //System.out.println(name); electset.setXueqi(jo.getString("xueqi")); electset.setXuenian(jo.getString("xuenian")); electset.setStartTime(jo.getString("startTime")); electset.setEndTime(jo.getString("endTime")); electset.setMenshu(jo.getInt("menshu")); electset.setIsReadDB(jo.getString("isReadDB")); return electset;}//\u8bbe\u7f6e\u5c5e\u6027\uff0c\u5e76\u4fdd\u5b58public boolean setElect(String path,String sets){ try { writeFile(path,sets); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; }}//\u8bfb\u6587\u4ef6\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32public String ReadFile(String path){ File file = new File(path); BufferedReader reader = null; String laststr = ""; try { //System.out.println("\u4ee5\u884c\u4e3a\u5355\u4f4d\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4e00\u6b21\u8bfb\u4e00\u6574\u884c\uff1a"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; //\u4e00\u6b21\u8bfb\u5165\u4e00\u884c\uff0c\u76f4\u5230\u8bfb\u5165null\u4e3a\u6587\u4ef6\u7ed3\u675f while ((tempString = reader.readLine()) != null) { //\u663e\u793a\u884c\u53f7 System.out.println("line " line ": " tempString); laststr = laststr tempString; line ; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return laststr;}\u5c06\u83b7\u53d6\u5230\u7684\u5b57\u7b26\u4e32\uff0c\u5165\u5e93\u5373\u53ef\u3002
\u56fe\u5462?\u8bf7\u628a\u56fe\u8865\u4e0a
1:定义一个实体类2:用json lib将json字符串转为Java对象
3:用jdbc或hibernate将java对象存入数据库
扩展阅读:java javascript ... java poi ... java入门 ... java读取txt文件 ... java 解析xml ... java 读取json文件 ... java workbook ... java快速读取js文件 ... java与javascript的区别 ...