wuzeyu
2024-06-11 a3b3610216dfeb82c729c914dbb2018da7eb54ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.dy.pipIrrGlobal.util;
 
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import java.nio.charset.Charset;
import java.util.List;
 
/**
 * @author ZhuBaoMin
 * @date 2023/12/21 11:03
 * @LastEditTime 2023/12/21 11:03
 * @Description
 */
public class LocalJsonUtil {
    /**
     * 从指定路径获取JSON并转换为List
     * @param path json文件路径
     * @param elementType List元素类型
     */
    public static  <T> List<T> getListFromJson(String path, Class<T> elementType) {
        ClassPathResource resource = new ClassPathResource(path);
        String jsonStr = IoUtil.read(resource.getStream(), Charset.forName("UTF-8"));
        JSONArray jsonArray = new JSONArray(jsonStr);
        return JSONUtil.toList(jsonArray, elementType);
    }
}