views:

284

answers:

1

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

+1  A: 

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.

Fletcher Moore
I'm actually parsing the feed with jQuery's $.getJSON.If you look at the feed, there are three groups of results because YQL is querying multiple tables. How would you loop through the feed and check the group and then display the results using jQuery? That is something I need to write by hand because the results need to be displayed in different ways.
Keith
I apologize for not understanding. Use something like http://github.com/jamespadolsey/prettyPrint.js to print out a display you can read. Then you should be able to figure out how to drill through the Javascript object to get the values you want.
Fletcher Moore
YQL's console gives a nice view of the JSON feed in both "formatted view" and "tree view" and I know how to drill down JSON feeds normally. My problem is the feed being generated by the query.multi table and how it is grouping the items in the JSON feed.
Keith
I created a simple test with your JSON feed which worked for separating the types into arrays. `feedObj` is what is passed to the `$.getJSON` callback.
Fletcher Moore
That is exactly what I needed to get me going. Thanks Fletcher!
Keith