根据 Json 路径 把 Json 转化成 对应对象的 List 集合 的 工具类
/**
* @Author: wangmx
* @Description: 读取 json 文件 转成 对应 集合 类型
*/
public class JsonUtil {
/**
* @param name json路径
* @return
*/
public String json(String name){
StringBuffer stringBuffer = new StringBuffer();
try {
InputStream stream = getClass().getClassLoader().getResourceAsStream(name);
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
}
String s = stringBuffer.toString();
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取 json 加强版
* @param jsons json 路径
* @param clazz 对象
* @param <T>
* @return
*/
public <T>List<T> jsonLists(String jsons, Class<T> clazz){
try {
//获取json
String json = this.json(jsons);
List<T> rateList = JSONObject.parseArray(json,clazz);
return rateList;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}