我正在开始一个简单的项目,使用Google Spreadsheets进行数据存储,我使用cljs-ajax通过JSON访问。 cljs-ajax获取JSON并将其转换为clojurescript数据结构。我想提取电子表格行信息,这些信息现在是嵌套数据结构的一部分,但是,我在操作数据方面遇到了一些困难。
虽然我可以直接访问行(见下文),但当我尝试将嵌套子集放入let
块时,如:let [entries (get-in response [:feed :entry])]
,我无法使用{{1使用entries
或map
的局部变量,即使我可以在其上调用for
和count
。访问嵌套数据的子集是否有一些限制作为我自己不知道的变量?任何建议都非常感谢。感谢。
first
要下载并试用此代码,我需要setup a repo,只需要; the request gets data and sends it to the handler function
(defn ajax []
(ajax/ajax-request
{:uri query1
:method :get
:handler handler
:format (ajax/json-request-format)
:response-format (ajax/json-response-format {:keywords? true})}))
; heres the handler
(defn handler [[ok response]]
(if ok
(let [entries (get-in response [:feed :entry])]
; these will all work showing that entries is seqable
; and that I can access the sub-data of an individual entry
(.log js/console (count entries))
; i can call first, second and nth
(.log js/console (str (first entries)))
(.log js/console (str (second entries)))
(.log js/console (str (nth entries 2)))
; i can directly access a nested bit of data
(.log js/console (str (get-in (first entries) [:gsx$suborder :$t])))
; map does not work
(map getentryinfo entries)
; for/let does not work
(for [entry entries]
(let [soil (get-in entry [:gsx$suborder :$t])
suborder (get-in entry [:gsx$suborder :$t])
polygons (get-in entry [:gsx$suborder :$t])]
(.log js/console soil))))
(.error js/console (str response))))
; getentry function called by the handler
(defn getentryinfo [entry]
""
(let [soil (get-in entry [:gsx$soil :$t])
suborder (get-in entry [:gsx$suborder :$t])
polygons (get-in entry [:gsx$points :$t])]
(.log js.console (str soil suborder))))
并在浏览器中打开lein cljsbuild auto