Clojure环wrap -json-params弄乱了JSON数组

Tom*_*oli 6 clojure compojure ring

我目前正在使用clojure中的一些REST API,我正在使用带有compojure的ring.middleware.format库来将jSON转换为clojure数据结构.

我遇到了一个很大的问题,因为发布到环形应用程序的JSON会将所有数组替换为数组中的第一个项目.IE它将把这个JSON发布到它

{
    "buyer":"Test Name",
    "items":[
        {"qty":1,"size":"S","product":"Red T-Shirt"},
        {"qty":1,"size":"M","product":"Green T-Shirt"}
    ],
    "address":"123 Fake St",
    "shipping":"express"
}
Run Code Online (Sandbox Code Playgroud)

对此

{
    "buyer": "Test Name",
    "items": {
        "qty": 1,
        "size": "M",
        "product": "Green T-Shirt"
    },
    "address": "123 Fake St",
    "shipping": "express"
}
Run Code Online (Sandbox Code Playgroud)

它适用于任何数组,包括数组是根元素时.

我在clojure中使用以下代码来返回json:

(defroutes app-routes
  (GET "/"
       []
       {:body test-data})
  (POST "/"
        {data :params}
        {:body data}))
        ;{:body (str "Printing " (count (data :jobs)) " jobs")}))

(def app
  (-> (handler/api app-routes)
      (wrap-json-params)
      (wrap-json-response)))
Run Code Online (Sandbox Code Playgroud)

GET路由没有正确的数组和输出问题,因此它必须是我获取数据或wrap-restful-params中间件的方式.

有任何想法吗?

Dhr*_*dna 3

我对ring-json-params 也有类似的问题。所以我最终使用原始请求正文并自己解析 JSON 字符串。

我使用了以下代码:

(defroutes app-routes
(POST "/"
    {params :body}
    (slurp params)))
Run Code Online (Sandbox Code Playgroud)

我使用 clj-json.core 库来解析 JSON。

希望这可以帮助。如果您找到更好的方法,请分享。我是 Clojure/Compojure 新手!!!