如何在Clojure中将JSON打印到文件?

时间:2022-11-19 21:45:20

I would like to store JSON content in files but using the pretty version.

我想将JSON内容存储在文件中但使用漂亮的版本。

Just to be clear, this is the normal JSON:

需要明确的是,这是正常的JSON:

{"b":2, "a":1}

This is the pretty version of it:

这是它的漂亮版本:

{
    "b": 2,
    "a": 1
}

Is there a way in Clojure to achieve this?

在Clojure中有没有办法实现这一目标?

1 个解决方案

#1


8  

Use the cheshire library found here and use the generate-string function with the pretty flag set to true

使用此处的cheshire库并使用generate-string函数并将pretty标志设置为true

Example

;; generate some JSON with pretty formatting
(generate-string {:foo "bar" :baz {:eggplant [1 2 3]}} {:pretty true})
;; {
;;   "foo" : "bar",
;;   "baz" : {
;;     "eggplant" : [ 1, 2, 3 ]
;;   }
;; }

#1


8  

Use the cheshire library found here and use the generate-string function with the pretty flag set to true

使用此处的cheshire库并使用generate-string函数并将pretty标志设置为true

Example

;; generate some JSON with pretty formatting
(generate-string {:foo "bar" :baz {:eggplant [1 2 3]}} {:pretty true})
;; {
;;   "foo" : "bar",
;;   "baz" : {
;;     "eggplant" : [ 1, 2, 3 ]
;;   }
;; }