当前位置 : 主页 > 网络编程 > 其它编程 >

java读取json格式的文件详解

来源:互联网 收集:自由互联 发布时间:2023-07-02
一、要解决这个问题首先要知道json格式是什么JSON格式比如学生有学号姓名性别等。用json表示则为JSON格式比如学生有学号姓名性别等。用json表示则为{"studno":"11111","studname":"wwww","studsex
一、要解决这个问题首先要知道json格式是什么JSON格式比如学生有学号姓名性别等。用json表示则为JSON格式比如学生有学号姓名性别等。用json表示则为{"studno":"11111","studname":"wwww","studsex":"男"}(各个字段都是字符型这代表一个学生的信息。如果多个呢[{"studno":"122222","studname":"wwww","studsex":"男"},{"studno":"11111","studname":"xxxx","studsex":"男"},{"studno":"33333","studname":"ssss","studsex":"男"}]这就是json格式。二、那如何操作json格式的文件呢这个更简单了说白了就是直接读写文件再把读出来的文件内容格式化成json就可以了。三、具体操作。1.我有一个实体类如下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;}}2.有一个json格式的文件存的就是他的信息如下Sets.json:{"xuenian":"2007-2008","xueqi":"1","startTime":"2009-07-19 08:30","endTime":"2009-07-22 18:00","menshu":"10","isReadDB":"Y"}3.具体操作./** 取出文件内容填充对象*/public ElectSet findElectSet(String path){ElectSet electsetnew ElectSet();String setsReadFile(path);//获得json文件的内容JSONObject joJSONObject.fromObject(sets);//格式化成json对象//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;}//设置属性并保存public boolean setElect(String path,String sets){try {writeFile(path,sets);return true;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}//读文件返回字符串public String ReadFile(String path){File file new File(path);BufferedReader reader null;String laststr "";try {//System.out.println("以行为单位读取文件内容一次读一整行");reader new BufferedReader(new FileReader(file));String tempString null;int line 1;//一次读入一行直到读入null为文件结束while ((tempString reader.readLine()) ! null) {//显示行号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;}//把json格式的字符串写到文件public void writeFile(String filePath, String sets) throws IOException {FileWriter fw new FileWriter(filePath);PrintWriter out new PrintWriter(fw);out.write(sets);out.println();fw.close();out.close();}4.调用使用在网站的controller里调用的//取出json对象public void GetElectSettings(HttpServletRequest request,HttpServletResponse response) throws Exception {ElectSet electsetnew ElectSet();String absPath request.getRealPath("\");String filePath absPath "public\sets\electSets.json";electsetbusinessService.findElectSets(filePath);//这里是调用大家自己改改我调用的业务层 的。JSONArray jsonItems new JSONArray();jsonItems.add(electset);JSONObject jonew JSONObject();jo.put("data", jsonItems);System.out.println(jo);request.setCharacterEncoding("utf-8");response.setContentType("application/json;charsetutf-8");PrintWriter out response.getWriter();out.print(jo);}//修改json文件public void ChangeElectSet(HttpServletRequest request,HttpServletResponse response) throws Exception {request.setCharacterEncoding("UTF-8");response.setContentType("text/json;charsetutf-8");log.info("reach ChangeElectSet");String json (String) request.getParameter("json").trim();log.info("Change ElectSet");log.info(json);ElectSet sets new ElectSet();JSONObject jsonObject JSONObject.fromObject(json);sets (ElectSet) JSONObject.toBean(jsonObject, ElectSet.class);if(sets.getIsReadDB()"false"){sets.setIsReadDB("否");}else{sets.setIsReadDB("是");}String changes"{"xuenian":"";//因为json的属性要用引号所以要用"转义一下changes sets.getXuenian() "","xueqi":"" sets.getXueqi() "","startTime":"" sets.getStartTime() "","endTime":"" sets.getEndTime() "","menshu":"" sets.getMenshu() "","isReadDB":"" sets.getIsReadDB() ""}";System.out.println(changes);String absPath request.getRealPath("\");String filePath absPath "public\sets\electSets.json";

 

转:https://blog.51cto.com/xiaodu1993/1255655

【文章原创作者:香港服务器 http://www.558idc.com/hk.html提供,感谢支持】
上一篇:Java虚拟机详解JVM常见问题总结
下一篇:没有了
网友评论