Clojure地图,怎么读

时间:2016-02-11 11:44:35

标签: json clojure maps

我是clojure的新手,我对这张地图有一个问题。

{: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])

我可以阅读地图的正文,但是当我制作时

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

我只是得到一个零响应。 我如何用clojure读取这个字段? 谢谢,抱歉我的英语不好。

1 个答案:

答案 0 :(得分:2)

正文是json,所以你需要首先使用json库来解码正文字符串,然后才能将它作为地图处理:

使用cheshire,Clojure的主要JSON库之一,您可以编写如下代码:

(require '[cheshire.core])

(-> location
    :body
    cheshire.core/parse-string
    (get-in ["coord" "log"]))