I'm stuck, (new to JavaScript) so I don't want to use Jquery. However I do need to do the following in JavaScript. It's a simple get/decode and loop array.
<?php
$url = "http://data.police.uk/api/forces"; // Make the url
$c = file_get_contents($url); // Get JSON
$forces = json_decode($c,true); // Decode JSON
foreach( $forces as $obj ) {
echo $obj['id'];
} ?> <!-- End Loop -->
Thank you for your support, you guys are great!
That API supports JSONP:
http://data.police.uk/api/forces?callback=your_callback
So you can just create a <script>
tag:
var script = document.createElement('script');
script.src = 'http://data.police.uk/api/forces?callback=your_callback';
script.type = 'text/javascript';
document.head.appendChild(script);
And register a global callback function that will run when that script loads:
window.your_callback = function(data) {
for (var i = 0; i < data.length; i++) {
...
}
};
But jQuery is nicer:
$.getJSON('http://data.police.uk/api/forces?callback=?', function(data) {
...
});
It is essentially writing a parser - but you might want to check this blog out: