Clojure使用JavaJSONPATH
我想在Clojure中使用 从自述文件来看,它看起来很简单,特别是因为它使用静态函数。因此,我复制了自述文件中的示例:Clojure使用JavaJSONPATH,clojure,clojure-java-interop,Clojure,Clojure Java Interop,我想在Clojure中使用 从自述文件来看,它看起来很简单,特别是因为它使用静态函数。因此,我复制了自述文件中的示例: (import '[com.jayway.jsonpath JsonPath Criteria Filter]) (JsonPath/parse "{\"a\":\"1\"}") ;; returns a com.jayway.jsonpath.internal.JsonContext (JsonPath/read (JsonPath/parse "{\"a\":\"1\
(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静态函数?为什么要将其作为此
依赖项:
[com.jayway.jsonpath/json-path“2.4.0”]
问题在于启用了谓词的varargs。在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"
user=>(导入'[com.jayway.jsonpath-jsonpath条件筛选谓词])
#
user=>(JsonPath/read“{\'a\':\'1\'}”“$.a”(插入数组谓词[])
"1"