• 【Java】【21】读写Json文件


    正文:

    1,通用读取方法,返回map

        public static List<Map> JsonRead(HttpServletRequest request, String path) {
            List<Map> maps = null;
            String dir = request.getSession().getServletContext().getRealPath(path);
            System.out.println(path);
            System.out.println(dir);
            if (dir == null) {
                System.out.println("找不到数据");
                return null;
            }
            try {
                File file = new File(dir);
                if (!file.exists()) {
                    file.createNewFile();
                }
                String str = FileUtils.readFileToString(file, "UTF-8");
                maps = (List) JSONArray.fromObject(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return maps;
        };

    2,先定义好实体类

    public static void test() {
        ObjectMapper mapper = new ObjectMapper();
        TypeFactory typeFactory = mapper.getTypeFactory();
        CollectionType collectionType = typeFactory.constructCollectionType(List.class, SpringMonthVo.class);
        List<SpringMonthVo> list = new ArrayList<>();
        try {
            InputStream is = SpringMonthVo.class.getResourceAsStream("/springMonth.json");
            list = mapper.readValue(is, collectionType);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    见我的博客的<13,是否是中国春节月>

    【Java】【19】Date Calendar相关 - 花生喂龙 - 博客园
    https://www.cnblogs.com/huashengweilong/p/10825007.html

    参考博客:

    java如何读写json文件 - <&nbsp/> - 博客园
    https://www.cnblogs.com/zhangdiIT/p/7590472.html

  • 相关阅读:
    CSP-S2-2019游记
    【BZOJ2301】【HAOI2011】Problem B
    【NOIp2017】宝藏
    【NOIp2016】天天爱跑步
    【NOIp2018】保卫王国
    【BZOJ2159】Crash的文明世界
    Van爆零赛总结
    【ZJOI2016】小星星
    【CF1025D】Recovering BST
    【HAOI2016】字符合并
  • 原文地址:https://www.cnblogs.com/huashengweilong/p/10909521.html
Copyright © 2020-2023  润新知