简体   繁体   English

Clojure地图,如何阅读

[英]Clojure map, how to read

I am new in clojure , and I have one problem with this map. 我是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}

I want to read fields but with get-in can not make this job.Only with 我想阅读字段,但是使用入门无法完成这项工作。

(get-in location [:body])

I can read the body of map but when I make 我可以阅读地图的正文,但是当我

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

I just get a nil response. 我只是没有回应。 How I can read this fields with clojure? 如何使用Clojure读取此字段? Thanks and sorry for my bad english. 谢谢,抱歉我的英语不好。

The body is json, so you need to first use a json library to decode the body string before being able to treat it as a map: 主体是json,因此您需要先使用json库解码主体字符串,然后才能将其视为地图:

Using cheshire , one of the major JSON libraries for Clojure, you could write this code like: 使用cheshire (Clojure的主要JSON库之一),您可以编写如下代码:

(require '[cheshire.core])

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM