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);
}
}
}
上一篇:JSON-6/6(生成json.org库文件 )
下一篇:JSON-4/6(JSON的生成与管理 )

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月10日 03时33分14秒