I'm a little new to JSON Syntax.
How can I extract the value of listeners from this JSON array?
I've tried with myvar.track.listeners but it doesn't work.
Can someone point me in the right direction?
The code that I'm using to get that value is:
function getInfo(artista, titolo) {
artista = artista.replace(" ","%20");
titolo = titolo.replace(" ","%20");
$.post("http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=31f2cd3c2530c87e110cc5212166d24c&artist="+artista+"&track="+titolo+"&format=json", {}, function(data) {
$("#listeners").html("<span class=\"span_listeners\">Ascoltatori: "+data.track.listeners+"</span>");
}, "json");
}
2 Answers 2
If you save this JSON to a variable response, then using response.track.listeners you should have the value you want.
The JSON you posted is not an array. JSON arrays are delimited by [...], as can be seen in the array track.toptags.tag in your document.
Comments
var my_json = {"track":{"id":"243317.....};
console.log(my_json.track.listeners);
...should be OK. But if you named your JSON object "var" it won't work (javascript keyword to declare a variable).
track.listenersis correct, but I'm fairly sure you can't name a variablevar. Check your javascript console for errors. A little more context on what you tried would be helpful too.var, that was just to explain what I was doing. However, I just wanted to get that value with jQuery. @xaro Ok.