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