Java 对象与json数据的转换,续写

来源:互联网 发布:java foreach 二维数组 编辑:程序博客网 时间:2024/06/03 22:48

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("文件已提交!");}}}


0 0
原创粉丝点击