[英]Clojure map, how to read
我是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 location [:body])
我可以閱讀地圖的正文,但是當我
(get-in location ["coord" "log"])
我只是沒有回應。 如何使用Clojure讀取此字段? 謝謝,抱歉我的英語不好。
主體是json,因此您需要先使用json庫解碼主體字符串,然后才能將其視為地圖:
使用cheshire (Clojure的主要JSON庫之一),您可以編寫如下代碼:
(require '[cheshire.core])
(-> location
:body
cheshire.core/parse-string
(get-in ["coord" "log"]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.