Clojure 使用 Java JsonPath

Clojure use Java JsonPath

我正在尝试在 Clojure

中使用 https://github.com/json-path/JsonPath

从自述文件看,它看起来很简单,尤其是因为它使用静态函数。所以我复制了自述文件中的示例:

(import '[com.jayway.jsonpath JsonPath Criteria Filter])


(JsonPath/parse "{\"a\":\"1\"}") ;; returns a com.jayway.jsonpath.internal.JsonContext

(JsonPath/read (JsonPath/parse "{\"a\":\"1\"}") "$.a")
;; (.read (JsonPath/parse "{\"a\":\"1\"}") "$.a")
;; Exception ->
;; 1. Caused by java.lang.IllegalArgumentException
;; No matching method: read
;; I tried variations of the above line
;; for some reason this seems to want to take `this` as first parameter - I cannot
;; figure out why, and cannot seem to be able to pass a valid value

如何从 Clojure 中调用这个 Java 静态函数?为什么它想要 this?

依赖性:[com.jayway.jsonpath/json-path "2.4.0"]

问题是 readPredicate 的可变参数。在 JVM 级别,您必须 将这些参数作为数组传递。使用 Java 编译器会处理它。

例如这有效:

user=> (import '[com.jayway.jsonpath JsonPath Criteria Filter Predicate])
#<Class@5a114a96 com.jayway.jsonpath.Predicate>
user=> (JsonPath/read "{\"a\":\"1\"}" "$.a" (into-array Predicate []))
"1"