现在操作json的jar 都是用的fastjson,
如果需要读取的json文件不在jar包里面,则可以这样获取到:
String path = this.getClass().getClassLoader().getResource("json/abc.json").getPath();//该目录是以resources目录为根目录
//文件内容直接转为String类型 String content = FileUtils.readFileToString(new File(path), "UTF-8");
JSONObject obj = JSONObject.parseObject(content)
否则就需要下面这样获取:
String path = "json/abc.json";//该目录是以resources目录为根目录
//文件内容直接转为String类型
InputStream is = this.getClass().getClassLoader().getResourceAsStream(path);
BufferedReader in = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line);
}
String input = buf
JSONObject obj = JSONObject.parseObject(buf.toString())