直接上代码
package org.util;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import java.io.FileInputStream;
import java.io.IOException;
public class JsonTool {
public String readFileToString(String filePath) {
String jsonString = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
jsonString = IOUtils.toString(fileInputStream, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return jsonString;
}
public JSONObject readFileToObject(String filePath) {
String jsonString = readFileToString(filePath);
JSONObject jsonObject = new JSONObject(jsonString);
return jsonObject;
}
}