Thread: JSON problem
View Single Post
Old 03-03-2013, 08:01 PM   PM User | #1
wingsgb
New to the CF scene

 
Join Date: Jul 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
wingsgb is an unknown quantity at this point
JSON problem

Hi all,

I have an understanding of HTML/CSS and a limited knowledge of PHP. I'm a member of a small gaming outfit were last year I designed and coded a new theme related website to gain abit of experience/ practice and gave the outfit something better than a forum.

Recently the developers of the PC game we play have just opened up beta testing to there API of ingame stats via JSON.

I would like to learn how to display this data in some form of a table.

Before I took to the forums asking all sorts of questions, I spent the weekend reading through whatever tutorials I could find. From them iv found the below code to see if I could do a basic call but its not loading any of the JSON data on my HTML page.

Code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function ajax_get_json(){
    var hr = new XMLHttpRequest();
    hr.open("GET", "Outfit_Stats.php", true);
    hr.setRequestHeader("Content-type", "application/json");
    hr.onreadystatechange = function() {
	    if(hr.readyState == 4 && hr.status == 200) {
		    var data = JSON.parse(hr.responseText);
			var results = document.getElementById("results");
			results.innerHTML = "";
			for(var obj in data){
				results.innerHTML += data[obj].name+ data[obj].alias+ data[obj].id+ data[obj].time_created+" <hr />";
			}
	    }
    }
    hr.send(null);
    results.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="results"></div>
<script type="text/javascript">ajax_get_json();</script>
</body>
</html>
Outfit_Stats.php
Code:
<?php
header("Content-Type: application/json");
$json_url = "http://census.soe.com/get/ps2-beta/outfit/?name=Immortal%20Serial%20Killers";
$jsonData = file_get_contents($json_url);
echo $jsonData;
?>\
Data
Code:
{
  "outfit_list": [
    {
      "id": "37509488620602819",
      "alias": "ISK",
      "name": "Immortal Serial Killers",
      "time_created": "1.353438835E9",
      "leader_character_id": "5428010618030994401"
    }
  ],
  "internal": false,
  "milliseconds": 6,
  "returned": 1
}
The JSON url i have used above was just a minimal data pull to see if it worked.

THIS LINK of data is what i want to eventually display.

Any help would be much appreciated
wingsgb is offline   Reply With Quote