无法从文件运行 clojure 库 (clojure.data.json)

问题描述

我无法使用添加到项目中的库。我不断收到此错误

CompilerException java.lang.RuntimeException: No such namespace: json,compiling:

库是 clojure.data.json 来解析 json。 我在 project.clj 依赖项中添加了库:

:dependencies [[org.clojure/clojure "1.10.0"]
             [org.clojure/data.json "2.0.1"]
             [ring "1.8.0"]]

在 core.clj 中,我需要该库:

(ns todo-list.core
(:require  [clojure.data.json :as json]
         [ring.adapter.jetty :as webserver]))

我正在使用 Atom,我可以直接评估一个函数。当我评估以下函数时,出现上述错误

(def posts (json/read-str (slurp "data.json") :key-fn keyword))

我在命令行中运行了命令:lein install。还是不行

谁能告诉我这是怎么回事?特别是我可以毫无问题地使用其他库,例如:clojure.string :as str

解决方法

在 Atom 中,您是否评估了 ns 表单(或者在添加了 ns 表单后,在保存后加载整个文件)?

您不能只计算包含 (json/read-str ..) 的表达式,除非您首先计算了 ns 形式并需要命名空间来创建别名。