- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我真的很难让它工作。
我正在查询 Flickr API,它返回了一组数组和散列,我可以通过循环向我的应用程序添加数据。
我已经开始编写一些代码来迭代 API 结果以创建集合。虽然它一直失败,但想知道你们中是否有人可以看到我是否以正确的方式进行此操作或发现我做错了什么:
def self.add_collection(collection)
puts "Adding collection..."
Collection.create!({
:flickr_id => collection['id'],
:title => collection['title'],
:description => collection['description']
})
puts "Added collection."
end
def self.add_all
tree = Flickr.collections_list
pp tree
puts "Got response from Flickr"
tree.each do |c|
c[1].each do |c|
add_collection(c)
if c["collection"]
add_collection(c['collection'][0])
end
end
end
end
我的 API 结果是:
{"collections"=>
{"collection"=>
[{"id"=>"57928212-7215447632914033423",
"title"=>"site.co.uk",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"collection"=>
[{"id"=>"57928212-721457634949706778",
"title"=>"Holidays",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"collection"=>
[{"id"=>"57928212-724157634943457291",
"title"=>"Standard",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576289846181735",
"title"=>"Di-ip",
"description"=>"11th-13th May 2008 - (-W)"},
{"id"=>"7215-90497324306",
"title"=>"O-ay",
"description"=>
"August 2005 - (P- T)"},
{"id"=>"721576290947297760",
"title"=>"Iceland Holiday",
"description"=>
"5- 2004 - (N-)"}]},
{"id"=>"57928212-742157629691182748",
"title"=>"1 - W-09",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576296835355462",
"title"=>"D-er",
"description"=>""},
{"id"=>"721576294683541696",
"title"=>"D-rea",
"description"=>""},
{"id"=>"721576296843554852",
"title"=>"D-gdom",
"description"=>""},
{"id"=>"721576297104465912",
"title"=>"Day 4 - Gilr-walk",
"description"=>""},
{"id"=>"721576294710474644",
"title"=>
"Day 5 - Calif-uth",
"description"=>""},
{"id"=>"721576249710489442",
"title"=>"Day 6 - Si-untain",
"description"=>""},
{"id"=>"721576429834618028",
"title"=>
"Day 7 - Unive-za",
"description"=>""},
{"id"=>"721576249834637138",
"title"=>"Day 8 - Se-Park",
"description"=>""},
{"id"=>"721576249836419418",
"title"=>"Day 9 - Lego-Park",
"description"=>""},
{"id"=>"721576249851123596",
"title"=>"Day 10 - Kn-re City",
"description"=>""},
{"id"=>"721576298541700916",
"title"=>"Day 11 & 12 - Disn-sort",
"description"=>""},
{"id"=>"721576249851713058",
"title"=>"Day 13,14 & 15 - La-as",
"description"=>""}]},
{"id"=>"57928212-721547629866347760",
"title"=>"2 - Mi-10",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576294866337538",
"title"=>"Day 0 - Arr-rica",
"description"=>""},
{"id"=>"721576298468655454",
"title"=>"Day 1 - M-a",
"description"=>""},
{"id"=>"721576299707347598",
"title"=>"Day 2 - Adven-n",
"description"=>""},
{"id"=>"721576430011649024",
"title"=>"Day 3 - W-un",
"description"=>""},
{"id"=>"721576304034790528",
"title"=>"Day 4 - Sil-",
"description"=>""},
{"id"=>"721576300354520246",
"title"=>"Day 5 - St-eum",
"description"=>""},
{"id"=>"721576300443090072",
"title"=>"Day 6 - Si-uis",
"description"=>""},
{"id"=>"721576300443098290",
"title"=>"Day 7 - Ho-rld",
"description"=>""},
{"id"=>"721576300448416804",
"title"=>"Day 8 - Indi-ach",
"description"=>""},
{"id"=>"721576300448431390",
"title"=>"Day 9 - -ca",
"description"=>""},
{"id"=>"721457629997482351",
"title"=>
"Day 10 - Mount O-eads",
"description"=>""},
{"id"=>"721576304059600702",
"title"=>"Day 11 - Vall-",
"description"=>""}]},
{"id"=>"57928212-721457630658165668",
"title"=>"3 - Intim-010",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576341693409808",
"title"=>
"Day 1 - Gre-n City",
"description"=>""},
{"id"=>"721576306547898798",
"title"=>"Day 1 - C-s",
"description"=>""},
{"id"=>"721576306924436584",
"title"=>"Day 2 - -d",
"description"=>""},
{"id"=>"721547630699845912",
"title"=>"Day 3 - Ran-vities",
"description"=>""},
{"id"=>"721576304714758818",
"title"=>"Day 4 - Lo-urg",
"description"=>""},
{"id"=>"721576304717785454",
"title"=>"Day 5 - Bu-urg",
"description"=>""},
{"id"=>"721576304717785576",
"title"=>"Day 6 - Kin-ion",
"description"=>""}]},
{"id"=>"57928212-742157631693570817",
"title"=>"4 - Bes-12",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576316493011733",
"title"=>
"Day 0 - Early -orld Park",
"description"=>""},
{"id"=>"721576316934409808",
"title"=>
"Day 1 - Grea-ity",
"description"=>""},
{"id"=>"721576431698474564",
"title"=>"Day 2 - Shijin-rk",
"description"=>""},
{"id"=>"721576341703216195",
"title"=>
"Day 3 - Happ-rk",
"description"=>""},
{"id"=>"721574631713251068",
"title"=>"Day 4 - Victo-g",
"description"=>""},
{"id"=>"721576314761492087",
"title"=>"Day 5 - Floral-ark",
"description"=>""},
{"id"=>"721576318148565932",
"title"=>"Day 6 - H-du",
"description"=>""},
{"id"=>"721576318148581382",
"title"=>"Day -ry",
"description"=>""},
{"id"=>"721576318419272104",
"title"=>
"Day 8 - Ha-Park",
"description"=>""},
{"id"=>"721576314820243661",
"title"=>
"Day 9 - Sha-ruise",
"description"=>""},
{"id"=>"721574631872193783",
"title"=>
"Day 10 - Fisherma-ervatory",
"description"=>""},
{"id"=>"721574631874073880",
"title"=>
"Day 11- Gongqin-ark",
"description"=>""},
{"id"=>"721576318474794697",
"title"=>"Day 12 - Hap-ark",
"description"=>""},
{"id"=>"721576318940558319",
"title"=>"Day 13 - -",
"description"=>""},
{"id"=>"721576319544111677",
"title"=>
"Day 14 - G-World",
"description"=>""},
{"id"=>"721576319624210803",
"title"=>
"Day 15 - Chi-or",
"description"=>""},
{"id"=>"721576319628749339",
"title"=>
"Day 16 - Kn-rld",
"description"=>""},
{"id"=>"721576319751048564",
"title"=>"Day 17 - Ha-hen",
"description"=>""},
{"id"=>"721576319749243775",
"title"=>"Day 18 - O-eing",
"description"=>""},
{"id"=>"721576319765947399",
"title"=>"Day 19 - Dis-ong",
"description"=>""}]},
{"id"=>"57928212-721576434943433385",
"title"=>"5 - T-13",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576349908483894",
"title"=>"Day 1 - Tai-ng",
"description"=>""},
{"id"=>"721576349954099233",
"title"=>"Day 2 - -",
"description"=>""},
{"id"=>"721576350042484738",
"title"=>"Day 3 - J-orld",
"description"=>""},
{"id"=>"721576350594662914",
"title"=>
"Day 4 - For-t",
"description"=>""},
{"id"=>"721576350545445863",
"title"=>"Day 5-dise",
"description"=>""},
{"id"=>"721576352944144798",
"title"=>
"Day 6 - L-na",
"description"=>""}]},
{"id"=>"57928212-721576435094962919",
"title"=>"6 - Japan 2013",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576350944001201",
"title"=>"Day 1 - Fli-dium",
"description"=>""},
{"id"=>"721576350494923653",
"title"=>"Day 2 - Tok-a",
"description"=>""},
{"id"=>"721576350497502859",
"title"=>"Day 3 - Tobu Zoo",
"description"=>""},
{"id"=>"721576351430344700",
"title"=>"Day 4 - Uni-an",
"description"=>""},
{"id"=>"721457635131070810",
"title"=>"Day 5 - Hira-mple",
"description"=>""},
{"id"=>"721576351444725534",
"title"=>"Day 6 - Yo-adise",
"description"=>""},
{"id"=>"721576351454178145",
"title"=>"Day 7 - Nag-nd",
"description"=>""},
{"id"=>"721547635164257344",
"title"=>"Day 8 - Hok-oro",
"description"=>""},
{"id"=>"721576354182539619",
"title"=>"Day 9 - Ru-ort",
"description"=>""},
{"id"=>"721576351472948421",
"title"=>"Day 10 - Jo-ea",
"description"=>""},
{"id"=>"721576351894974362",
"title"=>"Day 11 - -d",
"description"=>""},
{"id"=>"721576352035465038",
"title"=>"Day 12- Tok-kitori",
"description"=>""},
{"id"=>"721576352574508275",
"title"=>"Day 13- To-a",
"description"=>""},
{"id"=>"721576352746739360",
"title"=>"Day 14 - Tok-ea",
"description"=>""},
{"id"=>"721576354268544001",
"title"=>"Day 15 - --ea",
"description"=>""}]}]},
{"id"=>"57928212-721574628668300643",
"title"=>"Days out",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"collection"=>
[{"id"=>"57928212-721457628668352769",
"title"=>"One off's",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576308409580828",
"title"=>"ChThisip",
"description"=>"Sun 29th July 2012 (Neil T, Mandy T)"},
{"id"=>"721576306394290306",
"title"=>"Blackpool - 8th Jul 2012",
"description"=>
"(Neil T, MThisS, Ben K)"},
{"id"=>"721576299940667113",
"title"=>
"Legoland Thisne 2010",
"description"=>"This)"},
{"id"=>"721576294405676947",
"title"=>"SkThisl 1)",
"description"=>""},
{"id"=>"721576294391564267",
"title"=>"Canal Boating",
"description"=>
"SunThismas)"},
{"id"=>"721576294391564197",
"title"=>"SThisert",
"description"=>
"12th Jul 2003 - (PeThisrk T)"},
{"id"=>"721576294391560887",
"title"=>"BThises",
"description"=>
" 29thThiss A)"},
{"id"=>"721576290848461747",
"title"=>"Go Karting 2012",
"description"=>" 28th Jan 2012 for Neil's birthday"},
{"id"=>"721576349284451987",
"title"=>
"SThis A)",
"description"=>""}]},
{"id"=>"57928212-721574628668280287",
"title"=>"Theme Parks",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"collection"=>
[{"id"=>"57928212-721457634949768308",
"title"=>"TThisrk",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576334256718298",
"title"=>
"TThisP)",
"description"=>""}]},
{"id"=>"57928212-721576429626109372",
"title"=>"PlThisl",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"7215762945459430821",
"title"=>"PThis11",
"description"=>"(Neil T, Rick M, Jackie DM)"}]},
{"id"=>"57928212-721576249991000745",
"title"=>"GuThisld",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576296244407020",
"title"=>"GThis011",
"description"=>
"(NeiThiseorge)"}]},
{"id"=>"57928212-721576294990999301",
"title"=>"FThisd",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576294625410346",
"title"=>"FlThis0",
"description"=>"(NThis)"}]},
{"id"=>"57928212-721457628668340547",
"title"=>"DrThisor",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576294638932742",
"title"=>"DraThis010",
"description"=>"(NThisW)"}]},
{"id"=>"57928212-721547628668287225",
"title"=>"AltThisrs",
"description"=>"",
"iconlarge"=>
"http://farm8.staticflickr.com/7024/cols/72157628668287225_190361d603_l.jpg",
"iconsmall"=>
"http://farm8.staticflickr.com/7024/cols/72157628668287225_190361d603_s.jpg",
"set"=>
[{"id"=>"721576328264334688",
"title"=>
"FebThis P)",
"description"=>""},
{"id"=>"721576306265468434",
"title"=>"Alt-ers - 15th Jul 2012",
"description"=>"(N-g A)"},
{"id"=>"721576303726416660",
"title"=>"A-n 2012",
"description"=>
"(NThis K)"},
{"id"=>"721576294666253333",
"title"=>"AThis2010",
"description"=>"(Thisl H)"},
{"id"=>"721576294990697063",
"title"=>"AThis10",
"description"=>"(This)"},
{"id"=>"721576299940655063",
"title"=>
"This008",
"description"=>"(NThis)"},
{"id"=>"721576429990712153",
"title"=>
"Event 2011 - 31st Oct-1st Nov 2011",
"description"=>"(This)"},
{"id"=>"721576295594429835",
"title"=>"This 29th-31st Oct 2009",
"description"=>"(ThisF)"},
{"id"=>"721576291947499268",
"title"=>
"Behi-l 2006",
"description"=>"(This)"},
{"id"=>"721576295411694365",
"title"=>"Day 2010 - 21st Jun 2010",
"description"=>"This)"}]}]}]},
{"id"=>"57928212-721457628985775057",
"title"=>"Yearly Events",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"collection"=>
[{"id"=>"57928212-721576429836230144",
"title"=>"This",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576299818309211",
"title"=>"Here StThisees",
"description"=>""},
{"id"=>"721576296173454860",
"title"=>"Thisats",
"description"=>""}]},
{"id"=>"57928212-721576258985777547",
"title"=>"Birthdays",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"7215762899995365613",
"title"=>"This12",
"description"=>""}]}]},
{"id"=>"57928212-721576298356236274",
"title"=>"Misc",
"description"=>"",
"iconlarge"=>"/images/collection_default_l.gif",
"iconsmall"=>"/images/collection_default_s.gif",
"set"=>
[{"id"=>"721576295511800621",
"title"=>"This",
"description"=>""},
{"id"=>"721576295391565747",
"title"=>"This",
"description"=>""}]}]}]},
"stat"=>"ok"}
在这方面我真的需要一些帮助。我认为这是数组和散列的多维方面,以及如何创建可以循环遍历它以添加集合的东西。我现在需要忽略这些集合。稍后将添加集合。
更新:
一直在玩这个,试图慢慢弄清楚散列的每个部分是什么,并具有以下内容:
def self.add_all
tree = Flickr.collections_list['collections']
puts "Got response from Flickr"
tree.each do |c|
c.each do |d|
ap d.class
#add_collection(d)
end
end
当我运行它时,d.class
返回:
String < Object
Array < Object
我能否访问 Array 部分以便我可以继续迭代,因为我被告知我不能对 String 执行 .each(这是有道理的)。
有什么想法吗?
最佳答案
是的,这令人困惑。你需要知道的是
tree["collections"]["collection"]
id
、title
、description
等)collection
(不是collection
)的键,其值是一个子集合数组现在,根据这些准则,我们可以编写代码来遍历树。我们递归调用 traverse_collection
来处理子集合。
def traverse_collection(collection)
Collection.create! :flickr_id => collection["id"],
:title => collection["title"],
:description => collection["description"]
if collection["collection"]
for child in collection["collection"]
traverse_collection child
end
end
end
for collection in Flickr.collections_list["collections"]["collection"]
traverse_collection collection
end
关于ruby-on-rails - 循环访问来自 Flickr 的 API 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20689698/
我已经设置了 Azure API 管理服务,并在自定义域上配置了它。在 Azure 门户中 API 管理服务的配置部分下,我设置了以下内容: 因为这是一个客户端系统,我必须屏蔽细节,但以下是基础知识:
我是一名习惯 React Native 的新程序员。我最近开始学习 Fetch API 及其工作原理。我的问题是,我找不到人们使用 API key 在他们的获取语句中访问信息的示例(我很难清楚地表达有
这里有很多关于 API 是什么的东西,但是我找不到我需要的关于插件 API 和类库 API 之间的区别。反正我不明白。 在 Documenting APIs 一书中,我读到:插件 API 和类库 AP
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我正在尝试找出设计以下场景的最佳方法。 假设我已经有了一个 REST API 实现,它将从不同的供应商那里获取书籍并将它们返回给我自己的客户端。 每个供应商都提供单独的 API 来向其消费者提供图书。
请有人向我解释如何使用 api key 以及它有什么用处。 我对此进行了很多搜索,但得到了不同且相互矛盾的答案。有人说 API key 是保密的,它从不作为通信的一部分发送,而其他人则将它发送给客户端
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
谁能告诉我为什么 WSo2 API 管理器不进行身份验证?我已经设置了两个 WSo2 API Manager 1.8.0 实例并创建了一个 api。它作为原型(prototype) api 工作正常。
我在学习 DSL 的过程中遇到了 Fluent API。 我在流利的 API 上搜索了很多……我可以得出的基本结论是,流利的 API 使用方法链来使代码流利。 但我无法理解——在面向对象的语言中,我们
基本上,我感兴趣的是在多个区域设置 WSO2 API 管理器;例如亚洲、美国和欧洲。一些 API 将部署在每个区域的数据中心内,而其他 API 将仅部署在特定区域内。 理想情况下,我想要的是一个单一的
我正在构建自己的 API,供以下用户使用: 1) 安卓应用 2) 桌面应用 我的网址之一是:http://api.chatapp.info/order_api/files/getbeers.php我的
我需要向所有用户显示我的站点的分析,但使用 OAuth 它显示为登录用户配置的站点的分析。如何使用嵌入 API 实现仪表板但仅显示我的网站分析? 我能想到的最好的可能性是使用 API key 而不是客
我正在研究大公司如何管理其公共(public) API。我想到的是拥有成熟 API 的公司,例如 Google、Facebook、Twitter 和 Amazon。 这些公司向公众公开了许多不同的 A
在定义客户可访问的 API 时,以下是首选的行业惯例: a) 定义一组显式 API 方法,每个方法都有非常狭窄和特定的目的,例如: SetUserName SetUserAge Se
这在本地 deserver 和部署时都会发生。我成功地能够通过留言簿教程使用 API 资源管理器,但现在我已经创建了自己的项目并尝试访问我编写的第一个 API,它从未出现过。搜索栏旁边的黄色“正在加载
我正在尝试使用 http://ip-api.com/ api通过我的ip地址获取经度和纬度。当我访问 http://ip-api.com/json从我的浏览器或使用 curl,它以 json 格式返回
这里的典型示例是 Twitter 的 API。我从概念上理解 REST API 的工作原理,本质上它只是针对您的特定请求向他们的服务器查询,然后您会在其中收到响应(JSON、XML 等),很棒。 但是
我能想到的最好的标题,但要澄清的是,情况是这样的: 我正在开发一种类似短 url 的服务,该服务允许用户使用他们的 Twitter 帐户“登录”并发布内容。现在这项服务可以包含在 Tweetdeck
我正在设计用于管理评论和讨论线程的 API 方案。我想有一个点 /discussions/:discussionId 当您GET 时,它会返回一组评论和一些元数据。评论也许可以单独访问 /discus
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭去年。 Improve this quest
我是一名优秀的程序员,十分优秀!