Clojure - 无法在响应的正文中解析json

时间:2014-06-22 18:39:37

标签: json clojure

我在解析响应体中出现的json时遇到问题......我可以成功地从身体中获取值。但是,当我试图从身体的json中获取一些价值时,我无法做到。

这是我使用的功能:

(ns cs4261-server.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]
            [clojure.data.json :as json]
            [ring.util.response :as resp]
            [org.httpkit.client :as http]
            [clj-http.client :as client]
            [org.httpkit.server :as hs]
            [ring.middleware.json :refer [wrap-json-params]]
            [ring.middleware.jsonp :refer [wrap-json-with-padding]]))

(defn get-recipes [param]
  (let [req (str "http://api.yummly.com/v1/api/recipes?_app_id=78dccce1&_app_key=87460fca330c28f52a9603ababd5a54f&allowedCourse[]=course^course-Dinner&maxTotalTimeInSeconds=3600&" param)]
    (let [resp (client/get req)]
      (let [body (json/read-str (get resp :body))]
        (get body :matches)))))

(defroutes app-routes
  (GET "/" [] (json/write-str "This is just some start screen to appease myself."}))
  (GET "/somerecipe" {params :query-string} (get-recipes params))
  (GET "/recipes" [] (json/write-str { :message "This is some other different response"}))
  (route/resources "/")
  (route/not-found "This isn't the page you're looking for.."))

当我运行(keys body)attributiontotalMatchCountfacetCountsmatchescriteria

时,我就明白了

(vals body)让我:

{"html" "Recipe search powered by Yummly", "url" "http://www.yummly.com/recipes/", "text" "Recipe search powered by Yummly", "logo" "http://static.yummly.com/api-logo.png"}73458{}[{"ingredients" ["fresh basil" "green onions" "mayo" "grated parmesan cheese" "salt" "ground black pepper" "cooked chicken"], "totalTimeInSeconds" 2700.0, "sourceDisplayName" "Kalyn's Kitchen", "flavors" nil, "attributes" {"course" ["Salads"]}, "imageUrlsBySize" {"90" "http://lh4.ggpht.com/xggeGxIgwVNoYmmiIRN36BubOH9rCY40mXh710HI2iMcq7LA3oRMR_9CxZPk-R_5RvolER9H5K8FmHv0gqItJyg=s90-c"}, "smallImageUrls" ["http://lh4.ggpht.com/e7-jniVWaIcn2_d5-hT5jR52w-zdTB6dUYNP2QNwDI1Y9kVRUotX-2y0BKEkWKtrMtxgHH2fjGv7_wchojr_=s90"], "recipeName" "Chicken Salad with Basil and Parmesan", "id" "Chicken-salad-with-basil-and-parmesan-309576", "rating" 5} {"ingredients" ["dried breadcrumbs" "grated parmesan cheese" "eggs" "boneless skinless chicken breasts" "italian seasoning"], "totalTimeInSeconds" 2700.0, "sourceDisplayName" "AllRecipes", "flavors" {"sour" 0.16666666666666666, "salty" 0.8333333333333334, "sweet" 0.16666666666666666, "bitter" 0.5, "meaty" 0.6666666666666666}, "attributes" {}, "imageUrlsBySize" {"90" "http://lh4.ggpht.com/wSztxHluA4QpWg2Ij1LZIMFXl2lcEW05W1VBbV8nIPHz7bDyjmIaemTsyCQ3yFSD4iMgYkvvpFUUWXoKcHI4rGg=s90-c"}, "smallImageUrls" ["http://lh5.ggpht.com/xKUecmTW7W9-8go_7SiStOl4weFo7XXeNdJF9soWJ6q0KHM8S3sMHRzEUiprkkSLBYp4cOHTuANFMj-_RALz2w=s90"], "recipeName" "Simply Parmesan Chicken", "id" "Simply-Parmesan-Chicken-Allrecipes", "rating" 5} {"ingredients" ["safflower oil" "onion" "kosher salt" "freshly ground pepper" "cooked chicken" "sweet paprika"], "totalTimeInSeconds" 3000.0, "sourceDisplayName" "Big Girls Small Kitchen", "flavors" {"sour" 0.3333333333333333, "salty" 0.16666666666666666, "sweet" 0.6666666666666666, "bitter" 0.16666666666666666, "piquant" 0.3333333333333333, "meaty" 0.16666666666666666}, "attributes" {}, "imageUrlsBySize" {"90" "http://lh3.ggpht.com/UxAXD4WzOGsSRbRH1MGq8BiiDJn-CQ95khtRooSe8vQL9ISciXr1vBxie2YCsrzdiwVd-Kf9IQ7YpGtk_zmBf1o=s90-c"}, "smallImageUrls" ["http://lh4.ggpht.com/CUG6ENZ5ZaOi-gbcTkFZ9A123PAzek15qTk53pHTVPnC1PH92O-ayqt4PDPgp_OycTTLVgU4m8e-Kv7daA7lcxs=s90"], "recipeName" "Chicken Fritz", "id" "Chicken-Fritz-466963", "rating" 5} {"ingredients" ["fresh mozzarella" "fresh basil" "olive oil" "salt" "tomatoes" "balsamic vinegar" "cooked chicken"], "totalTimeInSeconds" 900.0, "sourceDisplayName" "Steamy Kitchen", "flavors" {"sour" 0.3333333333333333, "salty" 1.0, "sweet" 0.5, "bitter" 0.8333333333333334, "meaty" 1.0}, "attributes" {}, "imageUrlsBySize" {"90" "http://lh5.ggpht.com/MxMrfdlGI8sXJK0lLp3g-Y_FpJrx0JI3zx06jOMf_YUkLUzdDFNR-c7KKiwE10xP5PksrSsjcvf7qphDApWT=s90-c"}, "smallImageUrls" ["http://lh6.ggpht.com/YV6IbDkZqbl2dEd-s6yRU0qnXyUM3_QWimLHkqILnHg14w7ysKjdELus7EhvHq4QfW0QD1IPELMzJGXyOl3bdw=s90"], "recipeName" "Chicken Caprese", "id" "Chicken-Caprese-512479", "rating" 5} {"ingredients" ["coarse salt" "boneless chicken breast halves" "kalamata" "grape tomatoes" "olive oil" "capers" "freshly ground pepper"], "totalTimeInSeconds" 1800.0, "sourceDisplayName" "Martha Stewart", "flavors" {"sour" 1.0, "salty" 0.3333333333333333, "sweet" 0.8333333333333334, "bitter" 0.6666666666666666, "meaty" 0.16666666666666666}, "attributes" {"cuisine" ["mediterranean"]}, "imageUrlsBySize" {"90" "http://lh4.ggpht.com/hYtmp4SgiNq_OXhEz5D_WdjiBZYFcX3FXDcw0Pi_0G6PPC10rwVHCRoFfWO3Qy2jofDarFyJd2djy1OKmv8smRE=s90-c"}, "smallImageUrls" ["http://lh5.ggpht.com/2iliWjEertg18obLCM8hxQVCSme-rkazLrtp1lp_L9TzO7bwcSw83sLjOP5JxErrCyfdJQcw8nm6DwgPyruU-Q=s90"], "recipeName" "Mediterranean Chicken", "id" "Mediterranean-Chicken-Martha-Stewart", "rating" 5} {"ingredients" ["smoked paprika" "garlic salt" "meat" "boneless chicken skinless thigh" "extra-virgin olive oil"], "totalTimeInSeconds" 2700.0, "sourceDisplayName" "The Shiksa in the Kitchen", "flavors" nil, "attributes" {}, "imageUrlsBySize" {"90" "http://lh5.ggpht.com/c1WyAP36fwSxoaarTbNsx_AgGygEIw5Kz1BUoKoTYvbaVcvr_M4h4TYnW9J697_RN2Jy537lIsuTzS7mwPoGZg=s90-c"}, "smallImageUrls" ["http://lh4.ggpht.com/B1ldEB7Dsj8r45FYUMZmxFdybJN_eBc4rt2rxIbaCaMH8GpKVk97xzaiMbEDgSKDbKkrHVLUs6RPM6rKxjV_=s90"], "recipeName" "Smoked Paprika Chicken", "id" "Smoked-Paprika-Chicken-The-Shiksa-in-the-Kitchen-200675", "rating" 5} {"ingredients" ["cabbage" "flour" "fresh tomatoes" "lemon wedge" "eggs" "panko breadcrumbs" "vegetable oil" "salt" "tonkatsu sauce" "chicken breasts"], "totalTimeInSeconds" 3000.0, "sourceDisplayName" "Food Republic", "flavors" {"sour" 0.6666666666666666, "salty" 0.16666666666666666, "sweet" 0.16666666666666666, "bitter" 0.16666666666666666, "meaty" 0.8333333333333334}, "attributes" {}, "imageUrlsBySize" {"90" "http://lh5.ggpht.com/xKpWqdnK3BhVeiZG0Zg9FB12WVM_O7KXxlDE1S_uuArRooIFHvAYZIipmmdrh8tMgGqtUS_fb2utpzdq7V1Bbg=s90-c"}, "smallImageUrls" ["http://lh3.ggpht.com/fasJj35yJTUk1jZtrlwEGskqw5EpUN212J7U47vNHjic4C8uq6TXGOqqKKIp9qWvuHYgBKgn4YQdER36lzEnxQ=s90"], "recipeName" "Chicken Katsu", "id" "Chicken-Katsu-561368", "rating" 4} {"ingredients" ["soy sauce" "pineapple juice" "garlic powder" "brown sugar" "boneless skinless chicken breasts" "bourbon whiskey" "ground black pepper"], "totalTimeInSeconds" 1800.0, "sourceDisplayName" "AllRecipes", "flavors" {"sour" 0.3333333333333333, "salty" 0.8333333333333334, "sweet" 0.8333333333333334, "bitter" 0.3333333333333333, "meaty" 0.16666666666666666}, "attributes" {}, "imageUrlsBySize" {"90" "http://lh4.ggpht.com/D31Ftoc_U4gvv2U3ZmKV_K6d5GREXJI4IzdCzYRi5Q8MrvdzO9uvkVnIhRZfMvG1ywGi4rLq93wHcyXKTWobOFE=s90-c"}, "smallImageUrls" ["http://lh3.ggpht.com/cCDQ7v9da78XI_ITjktEkf0cxV4G-ddNVJ6WQcJVFoRjv3SzIv2g03R-99Qgm8bLl3uDnPwyRsbzooCdGngEcA=s90"], "recipeName" "Whiskey Chicken", "id" "Whiskey-Chicken-Allrecipes", "rating" 4} {"ingredients" ["fish sauce" "bibb lettuce" "fresh lime juice" "fresh cilantro" "water" "ground chicken" "yellow onion"], "totalTimeInSeconds" 1800.0, "sourceDisplayName" "Food Republic", "flavors" {"sour" 0.3333333333333333, "salty" 0.6666666666666666, "sweet" 0.16666666666666666, "bitter" 0.3333333333333333, "meaty" 0.6666666666666666}, "attributes" {}, "imageUrlsBySize" {"90" "http://lh5.ggpht.com/PQwNMMmYfYU3ux62Kuy8ag8TBdqyg1AG5gW9gKO_oxk1WtmJar-RMGIBGsHndI-ynsOSVoJWspuotGp3uKf6=s90-c"}, "smallImageUrls" ["http://lh5.ggpht.com/JA3sOmAMrrvdUAS3qpDVdxjtBPElJHuAxdZZs1jfbLs0RNVEBYvz7FsCaAEhCCZRgHzJBh6Iuh7rFMv-9DWf2bc=s90"], "recipeName" "Easy Chicken Lettuce Wraps", "id" "Easy-Chicken-Lettuce-Wraps-513151", "rating" 5} {"ingredients" ["baby spinach leaves" "shredded mozzarella cheese" "kosher salt" "white onion" "extra-virgin olive oil" "ground black pepper" "chicken breasts" "bow-tie pasta" "Philadelphia Cooking Creme" "minced garlic"], "totalTimeInSeconds" 3600.0, "sourceDisplayName" "Picky Palate", "flavors" {"sour" 0.16666666666666666, "salty" 0.16666666666666666, "sweet" 0.16666666666666666, "bitter" 0.16666666666666666, "meaty" 0.6666666666666666}, "attributes" {}, "imageUrlsBySize" {"90" "http://lh5.ggpht.com/oeLx_E9OmzThj-VGPmXCLQ2U2SHsMqzn3pY0WMfsom1FMA2dhU_HCCJ4x-UjM0CfuZVrtlQlG317JTL4iSlV9A=s90-c"}, "smallImageUrls" ["http://lh5.ggpht.com/NQIgfnIrP-31XEvU5V6Pz9xgVHO0xcD2PmYZsqq9x_-mmayQHBBMLQQj4vC3VFnBM61tbilMtg_8-sw23kqwPg=s90"], "recipeName" "Chicken Florentine Bowtie Pasta", "id" "Chicken-florentine-bowtie-pasta-334939", "rating" 5}]{"excludedIngredients" nil, "allowedIngredients" nil, "terms" nil}

所以在那里有钥匙和戒指,我应该能得到一些东西,但我不是。有任何想法吗?

1 个答案:

答案 0 :(得分:1)

:key-fn keyword传递给json/read-str以获取关键字密钥。 然后,您可以查询地图:匹配(get body :matches)或甚至(:matches body)

以下是REPL会话示例:

(require '[clojure.data.json :as json])

(def js
  (json/read-str "{\"a\": 7, \"b\": 13}"
                 :key-fn keyword))
(:a js)

;; Gives back 7

或没有key-fn

(def js
  (json/read-str "{\"a\": 7, \"b\": 13}"))

;; Keys are strings now, have to use (get map key)
(get js "a")

;; Gives back 7
相关问题