views:

128

answers:

1

I'm trying to iterate through some JSON results from the YQL geo.places table using the YQL jQuery plugin (http://plugins.jquery.com/project/jquery-yql) troubleshooting with the following code;

$.yql("select * from geo.places where text=#{placename}", { 
        placename: '90210' 
    }, 
    function(data) {
        var Results = data.query.results;
        $.each(Results.place, function(name, value) {
            alert(name + ":" + value);
        });
    });
});

Except, whenever there is more than one "place" in the results, the alert will spit back "0:[object][object]", "1:[object][object]", etc (for each place). Whenever there is only one place result, the alert will spit back all the names and values of just the one place (woeid, name, country, admin1, etc.)

Essentially I'd like to...

  1. input a zip code to YQL

  2. verify the city/state/country from results against user submitted data

  3. update latitude and longitude fields from the results

Thanks!

+1  A: 

If Yahoo returns a single place, it does so as a property of results, not as an array of places. If it's not an array, you can't iterate. So test whether it's an array; if it's not, make it one:

$.yql("select * from geo.places where text=#{placename}", { 
        placename: '90210' 
    }, 
    function(data) {
        var Places = data.query.results.place;
        if (!Places.length) Places = [Places];
        $.each(Places, function(index, place) {
            alert(index + ":" + place.name);
        });
    });
});
dansays