How to loop a json array and update links
Posted
by azz0r
on Stack Overflow
See other posts from Stack Overflow
or by azz0r
Published on 2010-04-14T09:12:23Z
Indexed on
2010/04/14
9:23 UTC
Read the original article
Hit count: 283
Hello,
On page load, I do one call to get the current status of all the favourite links (display the right message aka: click to subscribe, click to unsubscribe.
So far I have the following code:
$(InitFavorite);
function InitFavorite(){
var jList = $(".favourite_link");
var ids_to_check = {};//new Array();
$.each(jList, function () {
var id = this.id;
var object = id.split("_");
if (!ids_to_check[object[1]]) {
ids_to_check[object[1]] = [];
}
ids_to_check[object[1]].push(object[0]);
});
//console.log(ids_to_check);
$.ajax({
type: 'POST',
url: '/user/subscription/favourite-listing',
data: ids_to_check,
dataType: 'json',
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8");
}
},
error: function() {
alert(1);
},
success: function(returned_values) {
$.each(returned_values, function() {
console.log($(this));
})
}
});
}
My returned data is:
{"env":"development","loggedIn":true,"translate":{},"aaron":"{\"Playlist\":{\"10\":\"Stop Recieving Updates For This Playlist\"},\"Clip\":{\"26\":\"Recieve Updates For This Clip\",\"27\":\"Recieve Updates For This Clip\",\"28\":\"Recieve Updates For This Clip\",\"29\":\"Stop Recieving Updates For This Clip\",\"30\":\"Recieve Updates For This Clip\"}}"}
I would like it to loop through the data and for example update the class
<a href="/user/subscription/toggle/id/26/object/Clip" class="favourite_link" id="26_Clip">
<img src="/design/images/icon/subscribe.png"> Add Clip To Favourites</a>
However eaching through returned_values equals this in firebug:
["{", """, "P", "l", "a", "y", "l", "i", "s", "t", """, ":", "{", """, "1", "0", """, ":", """, "S", "t", "o", "p", " ", "R", "e", "c", "i", "e", "v", "i", "n", "g", " ", "U", "p", "d", "a", "t", "e", "s", " ", "F", "o", "r", " ", "T", "h", "i", "s", " ", "P", "l", "a", "y", "l", "i", "s", "t", """, "}", ",", """, "C", "l", "i", "p", """, ":", "{", """, "2", "6", """, ":", """, "R", "e", "c", "i", "e", "v", "e", " ", "U", "p", "d", "a", "t", "e", "s", " ", "F", "o", "r", " ", "T", "h", "i", "s", " ", "C", "l", "i", "p", """, ",", """, "2", "7", """, ":", """, "R", "e", "c", "i", "e", "v", "e", " ", "U", "p", "d", "a", "t", "e", "s", " ", "F", "o", "r", " ", "T", "h", "i", "s", " ", "C", "l", "i", "p", """, ",", """, "2", "8", """, ":", """, "R", "e", "c", "i", "e", "v", "e", " ", "U", "p", "d", "a", "t", "e", "s", " ", "F", "o", "r", " ", "T", "h", "i", "s", " ", "C", "l", "i", "p", """, ",", """, "2", "9", """, ":", """, "S", "t", "o", "p", " ", "R", "e", "c", "i", "e", "v", "i", "n", "g", " ", "U", "p", "d", "a", "t", "e", "s", " ", "F", "o", "r", " ", "T", "h", "i", "s", " ", "C", "l", "i", "p", """, ",", """, "3", "0", """, ":", """, "R", "e", "c", "i", "e", "v", "e", " ", "U", "p", "d", "a", "t", "e", "s", " ", "F", "o", "r", " ", "T", "h", "i", "s", " ", "C", "l", "i", "p", """, "}", "}"]
How would I successfully loop that json array? Many thanks
© Stack Overflow or respective owner