Skip to content

Instantly share code, notes, and snippets.

@paulusm
Last active December 14, 2015 04:59
Show Gist options
  • Save paulusm/5032464 to your computer and use it in GitHub Desktop.
Save paulusm/5032464 to your computer and use it in GitHub Desktop.
JSON demo
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Template</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style>
#output div{display:none;}
</style>
</head>
<body>
<script>
var url = "http://www.cems.uwe.ac.uk/~pmatthew/ATWD/jsonxml/index.php";
$(document).ready(function(){
$.ajax(url)
.done(function(data) {
var dataparsed = JSON.parse(data);
//console.log(dataparsed);
for(var i=0; i<dataparsed.data.value.length; i++){
$("#output").append("<div id='" + dataparsed.data.value[i].id + "'>"+ dataparsed.data.value[i].name + "</div>");
$("#" +(i+1)).fadeIn(i * 1200);
}
})
.fail(function() { alert("error"); }) });
</script>
<div id="output"></div>
</body>
</html>
<?php
//define some XML
$xml = <<<EOT
<root>
<data>
<value>
<id>1</id>
<name>Fred</name>
</value>
<value>
<id>2</id>
<name>Wilma</name>
</value>
<value>
<id>3</id>
<name>Barney</name>
</value>
<value>
<id>4</id>
<name>Betty</name>
</value>
</data>
</root>
EOT;
//create an object
$xmlobj = new SimpleXmlElement($xml);
//var_dump($xmlobj);
//now convert object to JSON
echo json_encode($xmlobj);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment