编辑代码


import java.util.*;

class Main {
	public static void main(String[] args) {
        Map<String, Object> tranMap = new HashMap<>();
        Map<String, Object> body = new HashMap<>();
        Map<String, Object> privateMap = new HashMap<>();
        Map<String, Object> aaa = new HashMap<>();

        aaa.put("bbb", "最终的值");
        aaa.put("ccc", "最终的值22");
        privateMap.put("aaa", aaa);
        body.put("private", privateMap);
        tranMap.put("body", body);

        String path = "body.private.aaa.ccc";
        Object result = getValueByPath(tranMap, path);
        System.out.println(result); // 输出: 最终的值
	}


     public static Object getValueByPath(Map<String, Object> map, String path) {
        if (map == null || path == null || path.isEmpty()) {
            return null;
        }
        
        String[] keys = path.split("\\.");
        Object current = map;
        
        for (String key : keys) {
            if (!(current instanceof Map)) {
                return null;
            }
            
            @SuppressWarnings("unchecked")
            Map<String, Object> currentMap = (Map<String, Object>) current;
            current = currentMap.get(key);
            System.out.println("key:"+key+" current:"+current);
            if (current == null) {
                return null;
            }
        }
        
        return current;
    }
}