我在 freebase 上从这里获取了一些 json (小心,你只能请求几次而不使用&key=your-key
)。
我想将响应转换为类似于此的内容:
({:case "Roe v. Wade", :plaintiffs ("Norma McCorvey"), :defendants ("Henry Wade"), :court "Supreme Court of the United States", :subjects ("Abortion" "Privacy"), :article "http://wp/en/68493"} ...)
这是我在使用 clojure.data.json/read-string 后想出的代码:
(defn extract-data [case]
{:case (case "name")
:plaintiffs (flatten (map #(get % "parties") (filter (fn [p] (some #(= (% "id") "/en/plaintiff") (p "role")))
(case "/law/legal_case/parties"))))
:defendants (flatten (map #(get % "parties") (filter (fn [p] (some #(= (% "id") "/en/defendant") (p "role")))
(case "/law/legal_case/parties"))))
:court (get-in case ["court" 0 "name"])
:subjects (map #(% "name") (case "subject"))
:article (get-in case ["/common/topic/article" 0 "source_uri" 0])})
(def response (-> query-uri
java.net.URL.
slurp
json/read-str))
(def case-data (map extract-data (response "result")))
提取数据似乎过于复杂,有没有更好的方法来做到这一点?这是可以使用 core.logic 的情况吗?如果是这样,怎么做?