1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 用fastjson将对象的列表转换成json格式 并读写.json文件

用fastjson将对象的列表转换成json格式 并读写.json文件

时间:2024-06-17 03:59:15

相关推荐

用fastjson将对象的列表转换成json格式 并读写.json文件

import com.alibaba.fastjson.JSON;import org.json.JSONArray;import org.json.JSONObject;public static void main(String[] args) {

// 返回的结果JSONObject result = new JSONObject();// 学生JSONArray studentJsonArray = new JSONArray(JSON.toJSONString(studentList));result.put("studentMap", studentJsonArray);// 时间表JSONObject pointTableJsonObject = new JSONObject(JSON.toJSONString(pointTable));result.put("pointTable", pointTableJsonObject);try {writeFile("D:\\8.json", result.toString());// ReadFile("D:\\8.json");System.out.println();} catch (IOException e) {e.printStackTrace();}

}

/*** 写文件到.Json* @param filePath* @param sets* @throws IOException*/public static 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();}/*** 读Json文件* @param path* @return*/public static Object ReadFile(String path) {File file = new File(path);BufferedReader reader = null;String laststr = "";try {reader = new BufferedReader(new FileReader(file));String tempString = 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 JSON.parse(laststr);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。