使用 jQuery 解析来自 YQL 的 JSON feed

发布于 2024-09-01 19:52:41 字数 1128 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2024-09-08 19:52:41

您不需要手动解析 JSON。这就是 JSON 的意义所在。使用 JSON.parse(yourJSONstring) 将其转换为 Javascript 对象。

编辑:实际上我不确定浏览器是否支持该浏览器。这是 jQuery 的方式:

http://api.jquery.com/jQuery.parseJSON/

Edit2:

var results = feedObj.query.results.results
for (var i = 0; i < results.length; i++) {
  if (results[i].photo) {
    // do something with photos
  } else if (results[i].item) {
    // do something with items
  } else {
    // do something with entry
  }
}

测试 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:

var results = feedObj.query.results.results
for (var i = 0; i < results.length; i++) {
  if (results[i].photo) {
    // do something with photos
  } else if (results[i].item) {
    // do something with items
  } else {
    // do something with entry
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文