增删改查本地JSON
2015-06-26 16:43
567 查看
存储数据到JSON文件:
User u = new User();MessageUtil.cache=”D:\\WSE\\Weixin\\”String path = MessageUtil.cache + "WebContent\\config\\user.json";JSONObject jo = JSONObject.fromObject(u);FileWriter writer = new FileWriter(path, true);writer.write(jo.toString() + ",\r\n");writer.close();存储时为续写模式,而非替换。读取本地JSON文件:
String path = MessageUtil.cache + "WebContent\\config\\user.json";String sets = ReadFile(path);JSONArray ja = JSONArray.fromObject("[" + sets + "]");// 格式化成json对象for (int i = 0; i < ja.size(); i++) {User u = new User();jo.get("KEY").toString();//KEY为User存储时保存的属性。Return u}ReadFile.java:
public static String ReadFile(String path) {String laststr = "";File file = new File(path);BufferedReader reader = null;try {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:
根据条件找到匹配对象,设置更新属性,然后移除源对象,添加新对象,最后在转为JSONARRAYString path = MessageUtil.cache + "WebContent\\config\\user.json";String sets = ReadFile(path);// 读取本地jsonJSONArray ja = JSONArray.fromObject("[" + sets + "]");// 格式化成json对象JSONObject newjo = null;for (int i = 0; i < ja.size(); i++) {JSONObject jo = ja.getJSONObject(i);if (jo.get("key").equals(key)) {//匹配条件User u = (User) JSONObject.toBean(jo, User.class);u.setToken(token);//需要更新的属性u.setPassword(password); //需要更新的属性newjo = JSONObject.fromObject(u);//转为JSON对象ja.remove(i);// 如果token无效,则删除该记录,i=i-1;}}ja.add(newjo);FileWriter fw = new FileWriter(path);fw.write("");// json文件fw.close();FileWriter writer = new FileWriter(path, true);// 重新写入jsonfor (int i = 0; i < ja.size(); i++) {writer.write(ja.get(i).toString() + ",\r\n");}writer.close();
删除本地JSON数据:
先转为JSONARRAY,进行移除,然后重新写入String path = MessageUtil.cache + "WebContent\\config\\user.json";String sets = ReadFile(path);// 读取本地jsonJSONArray ja = JSONArray.fromObject("[" + sets + "]");// 格式化成json对象JSONObject newjo = null;for (int i = 0; i < ja.size(); i++) {JSONObject jo = ja.getJSONObject(i);if (jo.get("key").equals(key)) {ja.remove(i);// 如果token无效,则删除该记录,}}FileWriter fw;try {FileWriter writer = new FileWriter(path, true);// 重新写入jsonfw = new FileWriter(path);fw.write("");// json文件fw.close();for (int i = 0; i < ja.size(); i++) {writer.write(ja.get(i).toString() + ",\r\n");}writer.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
相关文章推荐
- json与jsonp应用及其他ajax数据交互方式
- JSP/SERVLET入门教程--Servlet 使用入门
- 那些逐步掌握的JavaScript(开篇)
- js contains方法
- js中cookie的使用详细分析
- JSP EL表达式详细介绍
- Js获取当前日期时间及格式化操作
- 如何正确的加载和执行 JavaScript 代码
- Javascript模块化编程(require.js)
- JS Charts生成网页的柱状图,圆饼图以及曲线图
- javascript编译与运行机理(1)--
- javascript实现相同事件名称,不同命名空间的调用方法
- JavaScript中的test()方法
- JavaScript实现数组随机排序的方法
- js打开新窗口并且居中显示
- 回文数组(Rotate Array (JS))
- JS中Null与Undefined的区别--2015-06-26
- JavaScript让Textarea支持tab按键的方法
- 利用自定义属性实现js点击事件 委托
- javascript实现textarea中tab键的缩排处理方法