美文网首页
02-本地JSON读取

02-本地JSON读取

作者: 糖纸疯了 | 来源:发表于2021-11-18 10:10 被阅读0次

    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();
        }
    }
    

    5、课后习题

    1)XXXX


    相关文章

      网友评论

          本文标题:02-本地JSON读取

          本文链接:https://www.haomeiwen.com/subject/edjdtrtx.html