如何打印已解析为 JSON 字符串的 Clojure 向量的每一项?
How do I print each item of a Clojure vector that has been parsed to a JSON string?
我的应用程序的入口点从 stdin 读取,调用逻辑层来处理输入,returns 输出为 JSON 字符串(使用 cheshire lib)。
但是目前,我的输出是一个 JSONs 的向量,例如:
[{"person":{"active":true,"age":41}},{"person":{"active":false,"age": 39}}]
而且我需要在单独的一行中将每个 JSON 打印到控制台,例如:
{"person": {"active": true, "age": 41}}
{"person": {"active": false, "age": 39}}
这是我当前的代码:
(defn read-input! [file]
(with-open [r (io/reader file)]
(doall (map #(cheshire/parse-string % true)
(line-seq r)))))
(defn input-received [input]
(map logic/process-input input))
(defn -main
[& args]
(println (cheshire/generate-string (-> args first read-input! input-received))))
我试过 (apply println (cheshire/generate-string (-> args first read-input! input-received)))
。但是随后每个字符都打印在每一行。
与其将整个输出向量打印为一个 JSON 字符串,不如分别打印向量的每个元素:
(defn print-all [xs]
(dorun (map (comp println cheshire/generate-string) xs)))
(print-all [{"person" {"active" true "age" 41}} {"person" {"active" false "age" 39}}])
;; prints the following:
{"person":{"active":true,"age":41}}
{"person":{"active":false,"age":39}}
我的应用程序的入口点从 stdin 读取,调用逻辑层来处理输入,returns 输出为 JSON 字符串(使用 cheshire lib)。
但是目前,我的输出是一个 JSONs 的向量,例如:
[{"person":{"active":true,"age":41}},{"person":{"active":false,"age": 39}}]
而且我需要在单独的一行中将每个 JSON 打印到控制台,例如:
{"person": {"active": true, "age": 41}}
{"person": {"active": false, "age": 39}}
这是我当前的代码:
(defn read-input! [file]
(with-open [r (io/reader file)]
(doall (map #(cheshire/parse-string % true)
(line-seq r)))))
(defn input-received [input]
(map logic/process-input input))
(defn -main
[& args]
(println (cheshire/generate-string (-> args first read-input! input-received))))
我试过 (apply println (cheshire/generate-string (-> args first read-input! input-received)))
。但是随后每个字符都打印在每一行。
与其将整个输出向量打印为一个 JSON 字符串,不如分别打印向量的每个元素:
(defn print-all [xs]
(dorun (map (comp println cheshire/generate-string) xs)))
(print-all [{"person" {"active" true "age" 41}} {"person" {"active" false "age" 39}}])
;; prints the following:
{"person":{"active":true,"age":41}}
{"person":{"active":false,"age":39}}