package org.jeecg.common.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.jeecg.common.constant.AccountReportConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import java.io.File; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.*; /** * Created by JQ.bi on 2020.06.03 */ public class JsonResourceUtil { private static Logger logger = LoggerFactory.getLogger(JsonResourceUtil.class); private JsonResourceUtil() { } //filename 为文件名字 如 “/json/app_version_info.json” public static JSONObject getJsonObjFromResource(String filename) { JSONObject json = null; if (!filename.contains(".json")) { filename += ".json"; } try { ClassPathResource classPathResource = new ClassPathResource(filename); if (classPathResource.exists()) { InputStream inputStream = classPathResource.getInputStream(); String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8); json = JSON.parseObject(content); } else { logger.info("file not exist!"); } } catch (Exception e) { e.printStackTrace(); logger.info("readFileToString" + e.getMessage()); } return json; } //json文件转化为JSONArray public static JSONArray getJsonArrFromResource(String filename) { JSONArray json = null; if (!filename.contains(".json")) { filename += ".json"; } try { ClassPathResource classPathResource = new ClassPathResource(filename); if (classPathResource.exists()) { InputStream inputStream = classPathResource.getInputStream(); String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8); json = JSON.parseArray(content); } else { logger.info("file not exist!"); } } catch (Exception e) { e.printStackTrace(); logger.info("readFileToString" + e.getMessage()); } return json; } public static Map> jsonToMap(Object jsonObj) { JSONObject jsonObject = JSONObject.parseObject(jsonObj.toString()); Map> mapMap = new HashMap<>(); jsonObject.forEach((key, value) -> mapMap.put(key, JSONObject.parseObject(value.toString()))); return mapMap; } /** * 拼接字段 */ public static String joinFiled(Map> mapMap,JSONArray array) { StringBuilder sb = new StringBuilder(); array.forEach(it -> { if (mapMap.get(it.toString()).isEmpty()) { sb.append(it).append(","); } else { sb.append(mapMap.get(it.toString()).get("filed")).append(","); } }); return sb.deleteCharAt(sb.length() - 1).toString(); } /** * 拼接title */ public static List joinTitle(Map> mapMap,JSONArray array) { List titles = new ArrayList<>(20); array.forEach(it -> { if (mapMap.get(it.toString()).isEmpty()) { titles.add(it.toString()); } else { titles.add(mapMap.get(it.toString()).get("comment").toString()); } }); return titles; } /** * 拼接字段 */ public static String joinAllFiled(Map> mapMap) { StringBuilder sb = new StringBuilder(); mapMap.forEach((k, v) -> sb.append(v.get("filed")).append(",")); return sb.deleteCharAt(sb.length() - 1).toString(); } }