1、写作背景
总想走捷径的人,往往因找不到捷径而固步自封,一步不前!
2、参考网址
3、学习目的
- 记录JSON读取工具类
4、核心操作
1)内存分页工具
SpringBoot会存在一种情况,将自己注册到Eureka,有时候一下本地File的读取会失败
@Service // 使用的FastJson
public class JsonReadUtil {
public List<UserModel> getAppList() throws IOException {
ClassPathResource resource = new ClassPathResource("/mock/mock.json");
File file = resource.getFile();
String jsonResult = jsonRead(file);
JSONObject jsonObject = JSONObject.parseObject(jsonResult);
JSONArray infBody = jsonObject.getJSONArray("infBody");
List<UserModel> dataList = infBody.toJavaList(UserModel.class);
return dataList;
}
private String jsonRead(File file){
StringBuilder builder = new StringBuilder();
try (Scanner scanner = new Scanner(file,"utf-8")){
while (scanner.hasNextLine()){
builder.append(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return builder.toString();
}
}
网友评论