My method to access a json url is as follows and it works, but now I want to go through the json with a for and its ´.length´ but I couldn't.
The json from which I want to get the url is the following
http://www.jsoneditoronline.org/?id=74fa8799c027f3af0b2faf44ac1c9e47
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js</scr>
<div id="success">
<div id="artisturl"></div>
</div>
$.ajax({
type : 'GET',
url : 'https://api.spotify.com/v1/search',
data : {'q':'coldplay',
'type':'artist'
},
dataType : 'json',
success : function(data) {
$('#success #artisturl').html(data.artists.items[0].uri) ;
},
}) ;
And why use jQuery when you can use native JavaScript?
Here's a solution that doesn't require external libraries:
I know that you are already using jQuery, but I am in favor of using native tools whenever possible (although I understand that it goes according to taste). Personally, I don't find it more complex than the jQuery solution, and you are more aware of your code.
By the way, you don't need to parse the JSON, since it
data
's already an object.You can do with jquery's own each:
For example from this link: https://stackoverflow.com/questions/2342371/jquery-loop-on-json-data-using-each