代码之家  ›  专栏  ›  技术社区  ›  nha

Clojure使用Java JsonPath

  •  1
  • nha  · 技术社区  · 8 年前

    我想用 https://github.com/json-path/JsonPath 在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\"}") "$.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"]

    1 回复  |  直到 8 年前
        1
  •  3
  •   cfrick    8 年前

    问题是瓦拉格斯 Predicate 在 read . 在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"