SpringBoot项目读取json格式文件配置
private void parseJson(){ File jsonFile = ResourceUtils.getFile("classpath:serviceConfig.json");
String json = FileUtils.readFileToString(jsonFile);
JSONArray jsonArray = JSON.parseArray(json);
for (Object obj : jsonArray) {
JSONObject jobj = (JSONObject) obj;
String iso = jobj.getString("***");
}
}
方式二:
@Value("classpath:serviceConfig.json") private Resource resource;
private void parseJson(){
File file = areaRes.getFile();
String jsonData = this.jsonFileReader(file);
JSONObject jsonObject = JSONObject.parseObject(jsonData);
.....
}
private String jsonFileReader(File file){
Scanner scanner = null;
StringBuilder buffer = new StringBuilder();
try {
scanner = new Scanner(file, "utf-8");
while (scanner.hasNextLine()) {
buffer.append(scanner.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
return buffer.toString();
}
以上是 SpringBoot项目读取json格式文件配置 的全部内容, 来源链接: utcz.com/z/513493.html