ClojureScript:解析 Transit 响应

ClojureScript: parsing a Transit response

我这周开始学习 ClojureScript,我一直在解析 Transit 响应,我有这个功能:

(defn handler [response]
   (let [comment (:comment response)
         created_at (:created_at response)
         last_name (:last_name response)
         _ (.log js/console (str ">>> COMMENT >>>>> " comment))
        comments_div (.getElementById js/document "comments")]
      (.append comments_div comment)
      (.log js/console (str "Handler response: " response))))

并且控制台显示:

所以,"response" 看起来不错,但我无法使用 "response" 地图(我认为是地图)获取内容:

 comment (:comment response) or comment (get response :comment)

headers 表示响应是 "application/transit+json" 类型。我试过了:

     (ns blog.core
        (:require [cognitect.transit :as t])) 

       (def r (t/reader :json))

      let [parsed (t/read r response).... <--- inside the let block

但到目前为止运气不好。我需要解析 var "response"?

由于它不像地图那样工作,它可能是一个字符串。尝试检查响应类型。

(println (type response))

如果是字符串则:

(ns example
  (:require [clojure.data.json :as json]))
  (console.log ((json/read-str response) :comment))

这很好用:

 (ns blog.core
   (:require [domina :as dom]
        [ajax.core :refer [GET POST DELETE]]
        [cognitect.transit :as t]
        [bide.core :as r]))

(def r (t/reader :json))

(defn handler [response]
  (let [parsed (t/read r response)
        _  (.log js/console (str ">>> PARSED >>>>> " (type parsed) ">>>>" parsed))
    comment      (get parsed "comment")
    .... rest of the code...