2014-04-05 2 views
0

DB에서 간단한 예제를 실행하기 위해 SQL Korma를 사용하고 있으며이를 Cheshire를 사용하여 JSON으로 변환하려고합니다.SQL Korma 결과를 json으로 변환

이 결과는 1 레코드 만 반환되었지만 결과가 2 개 이상일 때 오류가 발생합니다. Y는 해시 MAP이므로

(x) 
=> [{:created_on "2014-04-05 13:19:47", :id 1, :description "Room 1"} {:created_on "2014-04-05 13:20:17", :id 2, :description "Room 2"} {:created_on "2014-04-05 13:20:20", :id 3, :description "Room 3"}] 

:

(def x get-rooms) 

(def y (get-room 1)) 

X가 입력 testproj.models.db이다 :

(defn get-room [id] 
    (first (select room 
      (where {:id id}) 
      (limit 1)))) 

(defn get-rooms [] 
    (select room)) 

데이터 : 여기

는 2 개 함수이다 :

(pr-str y) 
=> "{:created_on \"2014-04-05 13:19:47\", :id 1, :description \"Room 1\"}" 
,451,515,

JSON 변환하려고 : 기록의 양에 따라

(cheshire.core/generate-string x) 
JsonGenerationException Cannot JSON encode object of class: class testproj.models.db$get_rooms: [email protected] cheshire.generate/generate (generate.clj:147) 

(cheshire.core/generate-string y) 
=> "{\"created_on\":\"2014-04-05 13:19:47\",\"id\":1,\"description\":\"Room 1\"}" 

왜 korma가 반환 다른 종류의 (이 나를 이해하는 데 도움 것보다이) 둘째 - 나는 이것에 대해 어떻게 가야하나요?

답변

2

함수 호출이 누락 된 것 같습니다. 이것을 시도하십시오 :

(cheshire.core/generate-string (x)) 
+0

감사합니다. 왜이 기능 호출이 필요한지 빨리 설명 할 수 있습니까? 이것이 x가 defentity가되는 이유입니까? – Longestline

+1

함수 호출 결과가 아닌 x를 함수에 바인딩했습니다. x를 (def x (get-rooms))으로 변경하고 (generate-string x) – NielsK