Clojure中的map,如何读取

我是Clojure新手,我在这个map中遇到了一个问题。

{:status 200, :headers {Server openresty, Date Thu, 11 Feb 2016 11:35:11 GMT, Content-Type application/json; charset=utf-8, Transfer-Encoding chunked, Connection close, X-Source back, Access-Control-Allow-Origin *, Access-Control-Allow-Credentials true, Access-Control-Allow-Methods GET, POST}, :body {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":278.36,"pressure":1004,"humidity":65,"temp_min":276.05,"temp_max":280.15},"visibility":10000,"wind":{"speed":2.1,"deg":230},"rain":{"1h":0.2},"clouds":{"all":0},"dt":1455190219,"sys":{"type":1,"id":5091,"message":0.0549,"country":"GB","sunrise":1455175330,"sunset":1455210486},"id":2643743,"name":"London","cod":200}, :request-time 695, :trace-redirects [http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98], :orig-content-encoding nil}

我想读取字段,但是使用get-in不能完成这个工作。只有

(get-in location [:body])

我可以读取map的主体,但是当我执行

(get-in location ["coord" "log"])

时,我只会得到一个空值响应。 我如何使用Clojure读取这些字段? 谢谢,对我的糟糕英语表示抱歉。

点赞
用户1000286
用户1000286

正文是 json 格式,因此您需要先使用 json 库将正文字符串解码为 map 才能处理它:

使用 cheshire 这样的 Clojure 主要 JSON 库,您可以编写以下代码:

(require '[cheshire.core])

(-> location
    :body
    cheshire.core/parse-string
    (get-in ["coord" "log"]))
2016-02-12 00:21:29