使用 jQuery 解析来自 YQL 的 JSON feed
我使用 YQL 的 query.multi 来获取多个提要,这样我就可以使用 jQuery 解析单个 JSON 提要并减少我正在建立的连接数量。为了解析单个提要,我需要能够检查结果的类型(照片、项目、条目等),以便我可以以特定方式提取项目。由于项目嵌套在 JSON 提要中的方式,我不确定循环结果并检查类型,然后循环项目以显示它们的最佳方法。
这是一个 YQL (http://developer.yahoo.com/yql/console/ ) query.multi 示例,您可以看到三种不同的结果类型(条目、照片和项目),然后是嵌套在其中的项目:
select * from query.multi where queries=
"select * from twitter.user.timeline where id='twitter';
select * from flickr.photos.search where has_geo='true' and text='san francisco';
select * from delicious.feeds.popular"
或者这里是 JSON 提要本身:
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20query.multi%20where%20queries%3D%22select%20*%20from%20flickr.photos.search%20where%20user_id%3D'23433895%40N00'%3Bselect%20*%20from%20delicious.feeds%20where%20username%3D'keith.muth'%3Bselect%20*%20from%20twitter.user.timeline%20where%20id%3D'keithmuth'%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
我正在使用 jQuery 的 $.getJSON 方法
I am using YQL's query.multi to grab multiple feeds so I can parse a single JSON feed with jQuery and reduce the number of connections I'm making. In order to parse a single feed, I need to be able to check the type of result (photo, item, entry, etc) so I can pull out items in specific ways. Because of the way the items are nested within the JSON feed, I'm not sure the best way to loop through the results and check the type and then loop through the items to display them.
Here is a YQL (http://developer.yahoo.com/yql/console/) query.multi example and you can see three different result types (entry, photo, and item) and then the items nested within them:
select * from query.multi where queries=
"select * from twitter.user.timeline where id='twitter';
select * from flickr.photos.search where has_geo='true' and text='san francisco';
select * from delicious.feeds.popular"
or here is the JSON feed itself:
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20query.multi%20where%20queries%3D%22select%20*%20from%20flickr.photos.search%20where%20user_id%3D'23433895%40N00'%3Bselect%20*%20from%20delicious.feeds%20where%20username%3D'keith.muth'%3Bselect%20*%20from%20twitter.user.timeline%20where%20id%3D'keithmuth'%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
I am using jQuery's $.getJSON method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要手动解析 JSON。这就是 JSON 的意义所在。使用 JSON.parse(yourJSONstring) 将其转换为 Javascript 对象。
编辑:实际上我不确定浏览器是否支持该浏览器。这是 jQuery 的方式:
http://api.jquery.com/jQuery.parseJSON/
Edit2:
测试
results[i].photo
对象是否存在。如果存在,结果是一个数组,您可以循环遍历该数组并对其执行某些操作。You don't need to parse JSON by hand. That's the point of JSON. Use
JSON.parse(yourJSONstring)
to convert it into a Javascript object.Edit: Actually I'm not sure the browser support for that one. Here's the jQuery way:
http://api.jquery.com/jQuery.parseJSON/
Edit2:
Test for the existence of the
results[i].photo
object. If it exists, the result is an array which you can loop through and do something with.