用Clojure从本地文件中读取JSON?

发布于 2025-02-04 09:16:17 字数 125 浏览 2 评论 0原文

我很清楚如何从HTTP请求中解析JSON。但是我想在我的代码中使用一个JSON文件。

我试图在Google上找到解决方案,但我正在努力弄清楚如何从文件系统中读取本地JSON文件,

谢谢

Vinn

Im pretty clear on how to parse JSON from http requests. But I have a JSON file locally I would like to use within my code.

I have tried to find a solution on google but I am struggling to figure out how to read a local JSON file from the file system

Thanks

Vinn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2025-02-11 09:16:17

使用 clojure/data.json 库:

  • 将此依赖关系添加到project.clj :

[org.clojure/data.json“ 2.4.0”]

  • 将此要求添加到命名空间定义:

(:quare clojure.data.json.json:as json]))

{"name":"John", "age":30, "car":null}

并以此方式阅读:

(json/read-str (slurp "filename.json"))
=> {"name" "John", "age" 30, "car" nil}

Use clojure/data.json library:

  • Add this dependency into project.clj:

[org.clojure/data.json "2.4.0"]

  • Add this requirement into namespace definition:

(:require [clojure.data.json :as json])

  • Then use read-str with slurp. I made example file filename.json with this content:
{"name":"John", "age":30, "car":null}

and read it like this:

(json/read-str (slurp "filename.json"))
=> {"name" "John", "age" 30, "car" nil}
稳稳的幸福 2025-02-11 09:16:17

在Clojure中,您可以使用DEP:

cheshire {:mvn/version "5.10.1"}

导入类:

:require
[cheshire.core :as json]

并使用此功能并返回MAPP

(defn read-invoice [file-path]
  (with-open [rdr (io/reader file-path)]
    (json/parse-stream rdr true)))

In clojure you can use the dep:

cheshire {:mvn/version "5.10.1"}

import the class:

:require
[cheshire.core :as json]

and use this function and return you a mapp

(defn read-invoice [file-path]
  (with-open [rdr (io/reader file-path)]
    (json/parse-stream rdr true)))
过度放纵 2025-02-11 09:16:17

好吧,从HTTP请求到达的JSON和从本地文件到达的JSON有什么区别?我想真正的问题是“如何从本地文件中读取”,不是吗?

https://github.com/clojure/data.json”从字符串中读取JSON的方法

(def json-str (json/read-str "{\"a\":1,\"b\":{\"c\":\"d\"}}"))

这是如何使用

echo '{"a":1,"b":{"c":"d"}}' > /tmp/a.json

,让我们从文件中读取它:

(def from-file (slurp "/tmp/a.json"))
(def json-file (json/read-str from-file))

确保它们相同:

(when (= json-str json-file)
  (println "same" json-file))

它将打印“相同”和解析的JSON值。

Well, what's the difference between a json arriving from an http request and a json arriving from a local file? I suppose the real question then is "how to read from a local file", no?

Here is how to read a json from a string using clojure/data.json:

(def json-str (json/read-str "{\"a\":1,\"b\":{\"c\":\"d\"}}"))

Now, lets put the same string into a file

echo '{"a":1,"b":{"c":"d"}}' > /tmp/a.json

And lets read it from the file:

(def from-file (slurp "/tmp/a.json"))
(def json-file (json/read-str from-file))

Make sure they are the same:

(when (= json-str json-file)
  (println "same" json-file))

Which would print "same" and the parsed json value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文