Java 对象与json数据的转换,续写
2015-08-19 10:56
826 查看
Java 对象与json数据的转换,续写
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.struts2.ServletActionContext; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import cn.huiKey.manager.entity.Download; public class JsonControlUtils { /** * 写入json数据 * * @param filePath * @param appObject * @throws IOException */ public static void writeFile(String filePath, JSONObject appObject) throws IOException { File file = new File(filePath); // 如果文件不存在、则创建该文件 if (!file.exists()) { file.createNewFile(); } JSONObject jsonObj; // 获取JSON数据字符串 String str = ReadFile(filePath); if (str.length() > 0) { // 获取JSON对象 jsonObj = JSONObject.fromObject(str); // 获取JSON集合对象 JSONArray arr = jsonObj.getJSONArray("download"); //循环遍历,替换重复的,添加最新的数据 for(int i=0;i<arr.size();i++){ JSONObject object=arr.getJSONObject(i); if(appObject.getInt("appid")==object.getInt("appid")&& appObject.getInt("type")==object.getInt("type")&& appObject.getInt("channel_id")==object.getInt("channel_id")&& appObject.getString("ver").equals(object.getString("ver"))){ //移除之前的数据,添加新的数据 arr.remove(object); arr.add(appObject); i=arr.size()-1; }else if (i==arr.size()-1){ arr.add(appObject); } } // 向集合里面添加新的JSONObject对象 } else { //创建download json数据 JSONArray appArray = new JSONArray(); appArray.add(appObject); jsonObj = new JSONObject(); jsonObj.element("download", appArray); } //写入数据 FileWriter fileWriter = new FileWriter(filePath); PrintWriter out = new PrintWriter(fileWriter); out.write(jsonObj.toString()); out.println(); fileWriter.close(); out.close(); } /** * 读json数据 * * @param path * @return */ public static String ReadFile(String path) { File file = new File(path); BufferedReader reader = null; // 构造最后返回的json串 String laststr = ""; try { // 以行为单位读取文件内容,一次读一整行: reader = new BufferedReader(new FileReader(file)); String tempString = null; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 拼接数据信息 laststr = laststr + tempString; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return laststr; } /** * 获取全部数据 * * @param filePath * @param sets * @throws IOException */ public static List<Download> getAllDataByCount( String path) throws Exception { // 文件存储路径 // String path = "/download.json"; File dataFile = new File(path); // 如果不存在,创建新文件 if (!dataFile.exists()) { dataFile.createNewFile(); } List<Download> list = new ArrayList<Download>(); // 获取JSON数据的字符串 String JsonContext = ReadFile(path); JSONArray jsonArray; // 判断文件内是否有数据 if (JsonContext.length() > 0) { // 获取文件JSON对象 JSONObject json = JSONObject.fromObject(JsonContext); // 获取文件JSON数组对象 jsonArray = json.getJSONArray("download"); int size = jsonArray.size(); for (int i = 0; i < size; i++) { // 获取jsonObject数据对象 JSONObject jsonObject = jsonArray.getJSONObject(i); Integer id = jsonObject.getInt("id"); // 格式化时间 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date time = sdf.parse(jsonObject.getString("time")); Integer appid = jsonObject.getInt("appid"); Integer type = jsonObject.getInt("type"); Integer channel_id = jsonObject.getInt("channel_id"); String ver = jsonObject.getString("ver"); Integer count = jsonObject.getInt("count"); // 数据添加到list里面 list.add(new Download(id, time, appid, type, channel_id, ver, count)); } } return list; } /** * 删除文件 * * @param file */ public static void deleteFile(File file) { // 判断文件是否存在 if (file.exists()) { // 判断是否是文件 if (file.isFile()) { // 删除文件 file.delete(); // 否则如果它是一个目录 } else if (file.isDirectory()) { // 声明目录下所有的文件 files[]; File[] files = file.listFiles(); // 遍历目录下所有的文件 for (int i = 0; i < files.length; i++) { // 把每个文件用这个方法进行迭代 deleteFile(files[i]); } // 删除文件夹 file.delete(); } } else { System.out.println("文件已提交!"); } } }
相关文章推荐
- 普通java类调用spring注解bean
- Java抽象类和接口
- JAVA学习(八):JAVA文件编程
- JAVA学习(八):JAVA文件编程
- 从零开始学JAVA DAY2
- springmvc 中 Instantiation of bean failed实例化Bean失败错误
- 使用Java操作ElasticSearch1.7程序
- 使用 Spring Data JPA 简化 JPA 开发
- Java线程:创建与启动
- cordova 项目导入eclipse中不显示assets和config.xm的解决方法
- Java中Comparator接口和Comparable接口的使用
- Java Classloader总结
- 【SSH2(实用文章)】--Struts2文件上传和下载的例子
- java 枚举类型enum 的使用
- java constructor 在构造子类时,一定会调用到父类的构造方法 “ 私有属性被继承了?”问题
- Spring基于注解配置Bean
- java 获取spring配置文件中bean 对象
- jdk1.8.0_45源码解读——Map接口和AbstractMap抽象类的实现
- eclipse mars集成插件开发j2ee项目
- Java-Java反射机制的学习与思考