java 读取json 文件并输出json字符串
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.Reader; public class Main { public static String readJsonFile(String fliename) { FileReader fileReader = null; Reader reader = null; String jsonStr = ""; try { // 获取FileReader 文件对象 fileReader = new FileReader(fliename); // 获取 Buffered 读取对象 BufferedReader in = new BufferedReader(fileReader); String str; StringBuffer sb = new StringBuffer(); while ((str = in.readLine()) != null) { sb.append(str); } jsonStr = sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return jsonStr; } }
读取出来的json字符串可以转换为json对象
转换参考