当前位置:   article > 正文

读取JSON工具类(根据文件路径路径获取),返回一个字符串_public返回json字符串

public返回json字符串
1.定义一个工具类
public static String readJsonFile(String filePath) {
    String jsonStr = "";
    try {
        File jsonFile = new File(filePath);
        Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
        int ch = 0;
        StringBuilder sb = new StringBuilder();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        reader.close();
        jsonStr = sb.toString();
        return jsonStr;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

2.使用时在方法里直接调用该工具类  参数里添加文件的具体路径

//根据文件路径获取文件  读取json,返回一个字符串
String jsonStr = JsonUtil.readJsonFile("D:/files/student-class.json");
//转化成json数组
JSONArray result = JSONObject.parseArray(jsonStr);
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/977879
推荐阅读
相关标签
  

闽ICP备14008679号