
JSON-5/6(保存JSON到文件 )
发布日期:2021-05-14 04:14:30
浏览次数:13
分类:精选文章
本文共 4629 字,大约阅读时间需要 15 分钟。
JSON���������������
��������� JSONObject ������ JSONArray������������������������ String ������������������������������������������������������ JsJSON.toFile() ������������������������������������������ JSON ���������������������������package json04;import java.io.File;import org.json.JSONObject;public class WriteJSON { public static void main(String[] args) throws Exception { // ������ JSONObject JSONObject j1 = new JSONObject(); j1.put("name", "������"); j1.put("id", 998856); j1.put("sex", true); j1.put("phone", "15199978652"); // ��� JSONObject ������������ File file = new File("student.txt"); JsJSON.toFile(j1, file, "utf-8"); System.out.println("������������"); }}
��������������������������������� JSON
��������������������������� `{ ... }`���JsJSON.fromFile() ������������������������ JSONObject��������������������� `[ ... ]`������������������ JSONArray������������������������������������������ JSON ������������������package json04;import java.io.File;import org.json.JSONObject;public class ReadJSON { public static void main(String[] args) throws Exception { // ������������������������������������ JSON File file = new File("student.txt"); JSONObject j1 = (JSONObject) JsJSON.fromFile(file, "utf-8"); // ��� JSONObject ������������������������ String jsonstr = j1.toString(2); System.out.println(jsonstr); System.out.println("������������"); }}
JsJSON ���
JsJSON ��������������� JSON ������������������������������������ JSON ������������������������������������������ JSON ������������������ JsJSON ���������������������package json04;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import org.json.JSONArray;import org.json.JSONObject;public class JsJSON { public static void toFile(Object json, File file, String encoding) throws Exception { String jsonstr = null; if (json instanceof JSONObject) { jsonstr = ((JSONObject) json).toString(2); } else if (json instanceof JSONArray) { jsonstr = ((JSONArray) json).toString(2); } else { throw new Exception("��������� org.json.JSONObject ��� org.json.JSONArray"); } OutputStream outputStream = new FileOutputStream(file); try { encoding = encoding.toUpperCase(); if (encoding.equals("UTF-8")) { byte[] bom = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; outputStream.write(bom); } byte[] data = jsonstr.getBytes(encoding); outputStream.write(data); } finally { try { outputStream.close(); } catch (Exception e) { // ������������������ } } } public static Object fromFile(File file, String encoding) throws Exception { InputStream inputStream = new FileInputStream(file); try { int fileSize = (int) file.length(); byte[] data = new byte[fileSize]; int n = inputStream.read(data); int offset = 0; encoding = encoding.toUpperCase(); if (n > 3 && encoding.equals("UTF-8")) { if (data[0] == (byte) 0xEF && data[1] == (byte) 0xBB && data[2] == (byte) 0xBF) { offset = 3; // ��� 3 ������������ BOM } } String jsonstr = new String(data, offset, n - offset, encoding); // ������������������������������ JSONObject ������ JSONArray char firstChar = ' '; for (int i = 0; i < jsonstr.length(); i++) { if (jsonstr.charAt(i) != ' ' && jsonstr.charAt(i) != '\t' && jsonstr.charAt(i) != '\r' && jsonstr.charAt(i) != '{' && jsonstr.charAt(i) != '[') { firstChar = jsonstr.charAt(i); break; } } JSONObject jsonResult = null; JSONArray arrayResult = null; if (firstChar == '{') { jsonResult = new JSONObject(jsonstr); } else if (firstChar == '[') { arrayResult = new JSONArray(jsonstr); } return jsonResult != null ? jsonResult : arrayResult; } catch (Exception e) { throw new RuntimeException("������������������", e); } }}
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年04月10日 03时33分14秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
信息学奥赛一本通【题目索引 + 解答】
2021-05-12
什么时候需要重写HashCode()
2021-05-12
2021-04-23
2021-05-12
Linux编程基础之创建两个子进程而不创建孙子进程
2021-05-12
hadoop 分布式文件系统的计算和高可用
2021-05-12
【Linux】VMware Workstation 不可恢复错误: (vcpu-0)
2021-05-12
VS中 fatal error LNK1123: 转换到 COFF 期间失败 的解决方法
2021-05-12
关于Img标签在固定宽高的容器内部以图片比例缩放存在
2021-05-12
pyhton---异常处理的终极语法、网页访问基本读取、网页访问异常处理
2021-05-12
linux下编程出现 对'sem_wait'未定义的引用解决方案
2021-05-12
ant design pro v5去掉右边content区域的水印
2021-05-12
web_求和(练习)
2021-05-12
JavaScript——使用iterator遍历迭代map,set集合元素
2021-05-12
IAR调试卡顿的解决办法
2021-05-13
应用程序无法启动,应用程序的并行配置不正确完美解决方法
2021-05-13
强大的文字处理器——Nisus Writer Pro
2021-05-13
如何轻松适应从Windows到MacOS的过渡!Mac新手入门指南
2021-05-13
fcpx插件:25个假日主题专业设计
2021-05-13
fcpx插件:Block Party for Mac(53个视频转场插件)
2021-05-13
代码绘制五角形
2021-05-13