View Single Post
Old 10-04-2012, 09:36 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
the json response is all objects within objects - you just have to figure out which objects you want to access. For me, it looks something like this...

Code:
<head>
<style>
img{
height:200px;
width:200px;
}
</style>
</head>
<body>
<div id="results"></div>
<script type="text/javascript">
function showGame(o){
var thediv=document.getElementById("results")
var base=o.query.results.json.streams;
for (var i = 0; i < base.length; i++) {
thediv.innerHTML+="Name: "+base[i].channel.name+"<br>";
thediv.innerHTML+="Viewers: "+base[i].viewers+"<br>";
thediv.innerHTML+="URL: "+base[i].channel.url+"<br>";
thediv.innerHTML+="Status: "+base[i].channel.status+"<br>";
thediv.innerHTML+="Preview: <img src='"+base[i].preview+"'/><br>";
thediv.innerHTML+="<br>"
	}
}	

var myurl=encodeURIComponent("https://api.twitch.tv/kraken/streams?game=DayZ&limit=3")

scr=document.createElement("script");
scr.type="text/javascript"
scr.src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22'+myurl+'%2F%22&format=json&callback=showGame';
document.body.appendChild(scr);

</script>
</body>
[EDIT]: just noticed you wanted the other name field. have updated the code above to grab that one.

Last edited by xelawho; 10-04-2012 at 09:53 PM.. Reason: updated code
xelawho is offline   Reply With Quote