我怎样才能遍历我的结果?

时间:2016-11-08 19:18:30

标签: clojure

这就是我目前所拥有的:

{:total-pages 1, :total-results 1, :items [{:item-atributes {:title Tolkien  Calendar 2017, :product-group Book, :manufacturer Harper Voyager, :author J. R.
R. Tolkien}, :SalesRank 12016, :item-links [{:description Technical Details, :url https://www.amazon.com/Tolkien-Calendar-2017-J-R/dp/tech-data/0062566938
%3FSubscriptionId%3DAKIAI6FVBZ4SCQ3VMGCQ%26tag%3D215401-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0062566938} {:description Ad
d To Baby Registry, :url https://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3D0062566938%26SubscriptionId%3DAKIAI6FVBZ4SCQ3VMGCQ%26tag%3D215401
-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0062566938} {:description Add To Wedding Registry, :url https://www.amazon.com/gp/r
egistry/wedding/add-item.html%3Fasin.0%3D0062566938%26SubscriptionId%3DAKIAI6FVBZ4SCQ3VMGCQ%26tag%3D215401-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D3
86001%26creativeASIN%3D0062566938} {:description Add To Wishlist, :url https://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3D0062566938%26Su
bscriptionId%3DAKIAI6FVBZ4SCQ3VMGCQ%26tag%3D215401-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0062566938} {:description Tell A
Friend, :url https://www.amazon.com/gp/pdp/taf/0062566938%3FSubscriptionId%3DAKIAI6FVBZ4SCQ3VMGCQ%26tag%3D215401-20%26linkCode%3Dxm2%26camp%3D2025%26creati
ve%3D386001%26creativeASIN%3D0062566938} {:description All Customer Reviews, :url https://www.amazon.com/review/product/0062566938%3FSubscriptionId%3DAKIAI
6FVBZ4SCQ3VMGCQ%26tag%3D215401-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0062566938} {:description All Offers, :url https://ww
w.amazon.com/gp/offer-listing/0062566938%3FSubscriptionId%3DAKIAI6FVBZ4SCQ3VMGCQ%26tag%3D215401-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26cre
ativeASIN%3D0062566938}], :detail-page-url https://www.amazon.com/Tolkien-Calendar-2017-J-R/dp/0062566938%3FSubscriptionId%3DAKIAI6FVBZ4SCQ3VMGCQ%26tag%3D2
15401-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0062566938, :asin 0062566938}]}

我从结果中得到了这个并希望遍历它。

2 个答案:

答案 0 :(得分:0)

不,那不是XML。您可以使用get-in遍历嵌套结构。

答案 1 :(得分:-1)

正如@jmargolistvt所说,你有一个Clojure数据结构(嵌套地图和数组),而不是xml。

此外,下次请使用prn代替println或类似内容进行打印,因为保持双引号字符的重要性,例如" Harper Voyager"。

我简化了一些数据,并且有了这个:

(ns clj.core
  (:require 
    [tupelo.core :as t] 
    [clojure.pprint :as pp]
    )
  (:gen-class))
(t/refer-tupelo)

(def data
  {:total-pages 1, :total-results 1, :items 
   [ {:item-atributes 
          {:title "Tolkien  Calendar 2017", :product-group "Book", 
           :manufacturer "Harper Voyager" 
           :author "J. R.  R. Tolkien" }, :SalesRank 12016, :item-links [{:description "Technical Details" } ]
     } ] } )


(pp/pprint data)

(defn -main [& args]
)

产生:

~/clj > lein run    
{:total-pages 1,
 :total-results 1,
 :items
 [{:item-atributes
   {:title "Tolkien  Calendar 2017",
    :product-group "Book",
    :manufacturer "Harper Voyager",
    :author "J. R.  R. Tolkien"},
   :SalesRank 12016,
   :item-links [{:description "Technical Details"}]}]}

此时,您可以添加

(newline)
(doseq [item (data :items) ]
  (newline)
  (pp/pprint item)
  (newline)
  (spyx (item :SalesRank))
  (spyx (get-in item [:item-atributes :title])))

得到:

{:item-atributes
 {:title "Tolkien  Calendar 2017",
  :product-group "Book",
  :manufacturer "Harper Voyager",
  :author "J. R.  R. Tolkien"},
 :SalesRank 12016,
 :item-links [{:description "Technical Details"}]}

(item :SalesRank) => 12016
(get-in item [:item-atributes :title]) => "Tolkien  Calendar 2017"

您的project.clj必须包含此内容才能使(spyx ...)部分正常工作:

:dependencies [
  [tupelo "0.9.9"]  ...