0

我这周开始学习 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))))

控制台显示:

在此处输入图像描述

因此,“响应”看起来不错,但我无法使用以下方法从“响应”地图(我认为是地图)中获取内容:

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

标头说响应是“应用程序/传输+json”类型。我试过:

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

       (def r (t/reader :json))

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

但到目前为止还没有运气。需要我解析变量“响应”吗?

4

2 回答 2

2

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

(println (type response))

如果它是一个字符串,那么:

(ns example
  (:require [clojure.data.json :as json]))
  (console.log ((json/read-str response) :comment))
于 2018-03-04T03:34:33.423 回答
1

这工作正常:

 (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... 
于 2018-03-04T22:38:59.357 回答