如何在Clojure中将Enlive对象转换为JSON对象?

如何在Clojure中将Enlive对象转换为JSON对象?,json,object,clojure,type-conversion,enlive,Json,Object,Clojure,Type Conversion,Enlive,我对Clojure有点陌生,不知道如何将这个Enlive对象转换为JSON对象 我使用解析方法解析了一个XML文件,如下所示: (def xml-parser (parse "<Vehicle><Model>Toyota</Model><Color>Red</Color><Loans><Reoccuring>Monthly</Reoccuring><Owners><Ow

我对Clojure有点陌生,不知道如何将这个Enlive对象转换为JSON对象

我使用解析方法解析了一个XML文件,如下所示:

(def xml-parser
  (parse "<Vehicle><Model>Toyota</Model><Color>Red</Color><Loans><Reoccuring>Monthly</Reoccuring><Owners><Owner>Bob</Owner></Owners></Loans><Tires><Model>123123</Model><Size>23</Size></Tires><Engine><Model>30065</Model></Engine></Vehicle>"))
{:Vehicle {:Model "Toyota"
           :Color "Red"
           :Loans {:Reoccuring "Monthly"
                   :Owners {:Owner "Bob"}}
           :Tires {
                   :Model 123123
                   :Size 23}
           :Engine {:Model 30065}}
}
我希望能够将其转换为JSON对象,如下所示:

(def xml-parser
  (parse "<Vehicle><Model>Toyota</Model><Color>Red</Color><Loans><Reoccuring>Monthly</Reoccuring><Owners><Owner>Bob</Owner></Owners></Loans><Tires><Model>123123</Model><Size>23</Size></Tires><Engine><Model>30065</Model></Engine></Vehicle>"))
{:Vehicle {:Model "Toyota"
           :Color "Red"
           :Loans {:Reoccuring "Monthly"
                   :Owners {:Owner "Bob"}}
           :Tires {
                   :Model 123123
                   :Size 23}
           :Engine {:Model 30065}}
}
如果使用的词汇不完全准确,我深表歉意


我在转换这一步上遇到了麻烦。提前感谢您的帮助。

这应该和递归内容转换一样简单:

(defn process-rec [{:keys [tag content]}]
  {tag (if (string? (first content))
         (first content)
         (apply merge (map process-rec content)))})
或者像这样,通过更深入的分解:

(defn process-rec [{tag :tag [x :as content] :content}]
  {tag (if (string? x) x (into {} (map process-rec) content))})
例如:

user> (process-rec data)
;;=> {:Vehicle
;;    {:Model "Toyota",
;;     :Color "Red",
;;     :Loans {:Reoccuring "Monthly", :Owners {:Owner "Bob"}},
;;     :Tires {:Model "123123", :Size "23"},
;;     :Engine {:Model "30065"}}}

这应该和递归内容转换一样简单:

(defn process-rec [{:keys [tag content]}]
  {tag (if (string? (first content))
         (first content)
         (apply merge (map process-rec content)))})
或者像这样,通过更深入的分解:

(defn process-rec [{tag :tag [x :as content] :content}]
  {tag (if (string? x) x (into {} (map process-rec) content))})
例如:

user> (process-rec data)
;;=> {:Vehicle
;;    {:Model "Toyota",
;;     :Color "Red",
;;     :Loans {:Reoccuring "Monthly", :Owners {:Owner "Bob"}},
;;     :Tires {:Model "123123", :Size "23"},
;;     :Engine {:Model "30065"}}}

您的目标对象是Clojure嵌套贴图;它不是JSON字符串(即,您没有任何双引号)。此外,这些键都是Clojure关键字,而不是JSON字典键(字符串)。请添加您尝试过的代码以及失败的原因(例如错误、堆栈跟踪、日志等),以便我们对其进行改进。在中,您要求创建EDN(但您的示例在技术上是EDN,但看起来更像YAML的CLJ版本)。现在您要求使用JSON,但您的示例是EDN。上面链接的答案提供了一种方法来构建您在此要求的确切结果。您真正的问题是如何将EDN转换为JSON?您的目标对象是Clojure嵌套映射;它不是JSON字符串(即,您没有任何双引号)。此外,这些键都是Clojure关键字,而不是JSON字典键(字符串)。请添加您尝试过的代码以及失败的原因(例如错误、堆栈跟踪、日志等),以便我们对其进行改进。在中,您要求创建EDN(但您的示例在技术上是EDN,但看起来更像YAML的CLJ版本)。现在您要求使用JSON,但您的示例是EDN。上面链接的答案提供了一种方法来构建您在此要求的确切结果。您真正的问题是如何将EDN转换为JSON?