简体   繁体   中英

get json object with # in object name using jquery

  • i'm using the recentracks api from lastFm:

www.last.fm/api/show?service=278

  • i used these sources to see how to use it:

api.jquery.com/jQuery.getJSON/

www.hunlock.com/blogs/Mastering_JSON_%28_JavaScript_Object_Notation_%29

  • i'm fetching the json format:

http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=melroymusic&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback= ?

i want to retrieve the name of the artist

{"track":[{"artist":{"#text":"deadmau5","mbid":""}

Logically i would use

item.artist.#text

total code:

$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=melroymusic&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback=?",

function(data){
    $.each(data.recenttracks.track, function(i,item){
    $("#lastfmMenu").append("<div class='lastfmItem'><div class='lastfmText'>"+item.artist.#text+"</div>" + "<div class='lastfmDate'>"+item.name+"</div></div>");
    });
});

doesnt work

i haven't found a code that could retrieve an object with #

i also saw @attr object i also wanted to use

 "@attr":{"nowplaying":"true"}

i dont know how to solve it and couldnt find anything about it

look at http://mellroy.com/ to see how it's working out

Thank you in advance, really appreciated

Here's use of jQuery's each :

var json = {"track":[{"artist":{"#text":"deadmau5","mbid":""}]};

$.each(json.track, function (b, c) {
    console.log('Index' + b, c.artist['#text']); // index 0 deadmau5
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM