forked from kidgrow-microservices-platform

zhaoxiaohao
2020-04-07 e70bbf83e3231d1608dcd9c84a6129e86f64ef17
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.kidgrow.order.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
 
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: <br>
 * @Project: <br>
 * @CreateDate: Created in 2020/4/4 12:48 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
public class JsonUtils {
    public static JSONObject readJsonFromClassPath(String path) throws IOException {
 
        ClassPathResource resource = new ClassPathResource(path);
        if (resource.exists()) {
            String areaData =  IOUtils.toString(resource.getInputStream(), Charset.forName("UTF-8"));
            return (JSONObject)JSON.parse(areaData);
        } else {
            throw new IOException();
        }
    }
 
    public static Map<String,Object> getMap(String path){
        Map<String,Object> map = new LinkedHashMap();
        try {
            //路径
            ClassPathResource classPathResource = new ClassPathResource(path);
            //读取文件信息
            String str = IOUtils.toString(new InputStreamReader(classPathResource.getInputStream(),"UTF-8"));
            //转换为Map对象
            map = JSONObject.parseObject(str, LinkedHashMap.class);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
 
}