1. main函数
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, updateArguments(args));
String jsonStr = JsonUtil.ReadJsonFile( System.getProperty("user.dir") + "/data/a.json");
JSONObject result = JSONObject.parseObject(jsonStr);
System.out.println(result.toString());
}
2. 读取文件工具类
public class JsonUtil {
public static String ReadJsonFile(String path){
StringBuilder str = new StringBuilder();
String tempString = "";
try {
FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(inputStreamReader);
while ((tempString = reader.readLine()) != null) {
str.append(tempString);
}
}catch (Exception ignored){
}
return str.toString();
}
}
3. json文件
{
"widgetsBundle": {
"alias": "alarm_widgets",
"title": "Alarm widgets"
},
"widgetTypes": [
{
"alias": "A1",
"name": "B1"
},
{
"alias": "A2",
"name": "B2"
}
]
}