Pass values from a JSON to html via Jquery

Asked

Viewed 707 times

4

I am trying to take the JSON data from Twitch and manipulate it in html, in case I wanted to put together a simple list with the online channels, using Alert it shows the channels but I thought to print the list in a ul,li without having to touch the html. Can someone point me in the right direction?

$.ajax({
 type : "GET",
 dataType : "jsonp",
 url : "https://api.twitch.tv/kraken/streams?limit=25&offset=25",
 success: function(data){
   for(i=0;i<data.streams.length;i++){
   //alert(data.streams[i]._links.self);
   }
 }
});

1 answer

5


I believe that by "without touching the html" you mean putting the content on the page via javascript, right?

// transforme a lista no que você precisa (lista de li):
var listaDeLi = data.streams.map(function(elemento) {
    return "<li>" + elemento._links.self + "</li>";
});

// coloque o conteúdo gerado em algum lugar
$('#content').append("<ul>" + listaDeLi.join('') + "</ul>");
  • 1

    http://jsfiddle.net/qo6dcyt3/1/

  • just to complement, @Juliano, if I wanted to create a condition based on a value that is in json, you would use an if?

  • It depends on what you check, but if is a possibility. If it’s to take some elements out of the list depending on the condition, then I would use a filter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.