views:

262

answers:

4

Hi,

I am attempting to pase a a JSON data using the JQuery getJSON function. The REST query is:

http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text%20%3D%20%22london%22&format=json&jsoncallback=?

The script I'm using to parse 'data' to obtain the WOEID value doesnt seem to work below:

 $.getJSON("http://query.yahooapis.com/v1/public/yql?"+
                "q=select%20woeid%20from%20geo.places%20where%20text%20%3D%20%22"+
                "london"+
                "%22&format=json&jsoncallback=?",
        function(data){
  console.log("json: " + data);
  var datatmp = data;
          if(data.results[0]){
            var data = filterData(data.results.place[0]);
           }
         }
       );

Can anyone say what I'm doing wrong? link text

+2  A: 

In this line:

      if(data.results[0]){
        var data = filterData(data.results.place[0]);
       }

You check to see if results[0] exists but then you don't use it. I suspect your problem would be fixed by changing to this:

      if(data.results[0]){
        var data = filterData(data.results[0].place[0]);
       }
Bob
thanks for the pointers
nav
+3  A: 

Your code needed a few tweaks, here's an updated version:

$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
      "q=select%20woeid%20from%20geo.places%20where%20text%20%3D%20%22"+
      "london"+
      "%22&format=json&jsoncallback=json",
      function(data){
          if(data.query.results){
              $.each(data.query.results.place, function(i, v) {
                  console.log("woeid #" + i + ": " + v["woeid"]);
              });
          }
      });​

The results object is beneath query, so you need to go into there first, the above code iterates through the woeid's of the first place returned and alerts them...it's just a starter, not sure what you ultimately wanted to do with the woeid but hopefully this will get you started. You can see the above code working here.

Nick Craver
Well how can I compete with that answer?
Bob
Thanks a lot Nick. I appreciate your help with this.
nav
Hi Nick, thanks for the code and it works perfect except some scenarios when only one woeid is returned..??http://jsfiddle.net/g7N4U/Many Thanks,
nav
@nav - Strange that they change their structure in that case, anyway check for the single case, like this: http://jsfiddle.net/g7N4U/1/
Nick Craver
Thanks Nick for the single case :) It is strange how the json response changes its structure when returning just one result!
nav
+1  A: 

I have a question: can you access that URL (http://query.yahooapis.com/...) even if it's not in your domain? Doesn't that violate the "same origin policy"?

Yassin
jQuery uses JSONP with a callback function, so the same origin policy is not an issue.
salathe
+2  A: 

You have two key mistakes:

  1. The correct parameter for specifying the callback in the YQL URL is callback rather than jsoncallback
  2. The results are to be found in data.query.results… rather than data.results…

Also it is worth noting that there is a data.query.count value returned with the YQL results so you can see how many results were returned.

salathe
`jsoncallback` is to tell jQuery you're dealing with `jsonp`, this part is correct. See the docs for details: http://api.jquery.com/jQuery.getJSON/
Nick Craver
`jsoncallback` is specific to Flickr as used in the example on that page. YQL uses `callback`.
salathe
@salathe - Correction to my previous comment, jQuery's looking for `callback=` in any case, the json prefix (or any prefix) is optional. You can see the relevant jQuery source here: http://github.com/jquery/jquery/blob/master/src/ajax.js#L212 As you can see in my answer (click the example link) his `jsoncallback` works since anything `callback=` matches on both ends :)
Nick Craver