欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解析yml文件 转换 Map

程序员文章站 2024-01-30 22:05:04
...

起因

有一个需求是把一个yml文件解析成HashMap格式的数据,文件内容如下

spring:
  datasource:
    serviceDB:
      jdbc-url: jdbc:mysql://127.0.0.1:3306/serviceDB
      password: test
      minimum-idle: 1
      idle-timeout: 60000
      maximum-pool-size: 150
      username: root
    cluster:
       - key: bi
         jdbc-url: jdbc:mysql://127.0.0.1:3306/test
         password: test
         minimum-idle: 1
         idle-timeout: 60000
         maximum-pool-size: 150
         username: root
       - key: his
         jdbc-url: jdbc:mysql://127.0.0.1:3306/his
         password: test
         minimum-idle: 1
         idle-timeout: 60000
         maximum-pool-size: 150
         username: root
 
config:
  schedul:
    onOff: false
  orderGen:
     masterWorkerId: 1
     backupWorkerId: 2

解析得到的结果如下所示:

{
  "spring.datasource.serviceDB.jdbc-url": "jdbc:mysql://127.0.0.1:3306/serviceDB",
  "spring.datasource.serviceDB.password": "test",
  "spring.datasource.serviceDB.username": "root",
  "spring.datasource.cluster[0].key": "bi",
  "spring.datasource.cluster[1].maximum-pool-size": "150",
  "config.schedul.onOff": "false",
  "spring.datasource.cluster[0].password": "test",
  "spring.datasource.cluster[0].maximum-pool-size": "150",
  "spring.datasource.cluster[1].username": "root",
  "spring.datasource.serviceDB.idle-timeout": "60000",
  "config.orderGen.backupWorkerId": "2",
  "spring.datasource.cluster[1].idle-timeout": "60000",
  "spring.datasource.serviceDB.minimum-idle": "1",
  "spring.datasource.cluster[0].username": "root",
  "spring.datasource.cluster[1].key": "his",
  "config.orderGen.masterWorkerId": "1",
  "spring.datasource.cluster[0].minimum-idle": "1",
  "spring.datasource.cluster[0].idle-timeout": "60000",
  "spring.datasource.cluster[1].password": "test",
  "spring.datasource.cluster[0].jdbc-url": "jdbc:mysql://127.0.0.1:3306/test",
  "spring.datasource.serviceDB.maximum-pool-size": "150",
  "spring.datasource.cluster[1].minimum-idle": "1",
  "spring.datasource.cluster[1].jdbc-url": "jdbc:mysql://127.0.0.1:3306/his"
}

实现思路

第一步:使用yml读取文件

Yaml yaml = new Yaml();
Map<String, Object> testMap =  
        yaml.load( new BufferedReader(new FileReader("test.yml")));
       

但是此时得到的testMap不是我们想要的结果,testMap的内容如下:

{"spring":{"datasource":{"serviceDB":{"jdbc-url":"jdbc:mysql://127.0.0.1:3306/serviceDB","password":"test","minimum-idle":1,"idle-timeout":60000,"maximum-pool-size":150,"username":"root"},"cluster":[{"key":"bi","jdbc-url":"jdbc:mysql://127.0.0.1:3306/test","password":"test","minimum-idle":1,"idle-timeout":60000,"maximum-pool-size":150,"username":"root"},{"key":"his","jdbc-url":"jdbc:mysql://127.0.0.1:3306/his","password":"test","minimum-idle":1,"idle-timeout":60000,"maximum-pool-size":150,"username":"root"}]}},"config":{"schedul":{"onOff":false},"orderGen":{"masterWorkerId":1,"backupWorkerId":2}}}

第二步:得到testMap后需要进行进一步的解析,得到需要的结果,这个时候就要使用递归解析了,需要注意的是对于yml中数组的解析要重点处理

核心代码如下:

 /**
     * 
     * @Title: json2propMap 
     * @Description: 解析json 转换 Map
     * @param jsonObject
     * @return  
     * @throws 
     *
     */
    public static Map<String, Object> json2propMap(JSONObject jsonObject){
        String tmpKey = "";
        String tmpKeyPre = "";
        Map<String, Object> configMap = new HashMap<String, Object>();
        json2prop(jsonObject, tmpKey, tmpKeyPre, configMap);
        return configMap;
    }
    
    /**
     * 
     * @Title: json2prop 
     * @Description: 递归解析
     * @param jsonObject
     * @param tmpKey
     * @param tmpKeyPre
     * @param configMap  
     * @throws 
     *
     */
    private static void json2prop(JSONObject jsonObject, String tmpKey, String tmpKeyPre, Map<String, Object> configMap) {
        Iterator<String> iterator = jsonObject.keySet().iterator();
        while (iterator.hasNext()) {
            // 获得key
            String key = iterator.next();
            String value = jsonObject.getString(key);
            Object valueObject = null;
            try {
                valueObject = JSONObject.parse(value);
            } catch (Exception e) {
                // 如果解析出错,就说明已经到头了,放入map然后继续解析
                configMap.put(tmpKey + key, value);
                continue;
            }
            // 如果是集合,需要特殊解析
            if (valueObject instanceof Collection<?>) {
                List<?> list = (List<?>)valueObject;
                tmpKeyPre = tmpKey;
//                tmpKey += key;
                for (int i = 0; i < list.size(); i++) {
                    String itemKey = tmpKey + key + "["+i+"]" + ".";
                    JSONObject itemValue = (JSONObject)list.get(i);
                    json2prop(itemValue, itemKey, tmpKeyPre, configMap);
                }
            } else if (valueObject instanceof JSONObject) {
                JSONObject jsonStr = JSONObject.parseObject(value);
                tmpKeyPre = tmpKey;
                tmpKey += key + ".";
                json2prop(jsonStr, tmpKey, tmpKeyPre, configMap);
                tmpKey = tmpKeyPre;
            } else {
                configMap.put(tmpKey + key, value);
                continue;
            }
        }
    }

第三步:得到最终结果,终极代码如下:

public static void main(String[] args) throws FileNotFoundException {
        Yaml yaml = new Yaml();
        Map<String, Object> testMap = yaml.load( new BufferedReader(new FileReader("test.yml")));
        System.out.println(JsonUtil.toJson(testMap));
        JSONObject jsonObject = JSONObject.parseObject(JsonUtil.toJson(testMap));
        // 递归解析Map
        testMap = JsonUtil.json2propMap(jsonObject);
        System.out.println(JsonUtil.toJson(testMap));
    }