3

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

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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

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]);
       }

1 Comment

You're close, but the issue is with how you're accessing the JSON structure. Yahoo's YQL response structure isn't data.results.place[0].
3

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.

3 Comments

jsoncallback is to tell jQuery you're dealing with jsonp, this part is correct. See the docs for details: api.jquery.com/jQuery.getJSON
jsoncallback is specific to Flickr as used in the example on that page. YQL uses callback.
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: 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 :)
2

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"?

1 Comment

jQuery uses JSONP with a callback function, so the same origin policy is not an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.