Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的 mongodb 模式有一个 JSON 层次结构。当我从 compojure 获取字段参数时,哈希是平点符号,如{"a.b" 1, "a.c" 2, "d.e" 3}. 我想使用 monger 来插入数据,但这需要一个真正的分层格式,如{:a {:b 1, :c 2}, :d {:e 3}}. 有没有办法自动从一种转换到另一种?
{"a.b" 1, "a.c" 2, "d.e" 3}
{:a {:b 1, :c 2}, :d {:e 3}}
我所知道的没有什么自动的,但是手动进行转换非常简单:
=> (require 'clojure.string) => (defn nest-keys [x] (reduce (fn [m [k v]] (assoc-in m (map keyword (clojure.string/split k #"\.")) v)) {} x)) => (nest-keys {"a.b" 1 "a.c" 2 "d.e" 3}) {:d {:e 3}, :a {:c 2, :b 1}}