How can I analyze JSON data from an API call?

advertisements

Here's what I have at the moment:

<?php
    $string = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=##################################&steamids=76561198040884950");
    $json=json_decode($string);
?>

<script type="text/javascript">
        alert("<?php echo $json; ?>");
</script>

All I'm trying to do at this stage is receive the JSON information. I'm quite new to jQuery and PHP so not sure where I'm going wrong.


By using json_decode you're converting the JSON from a string (which can be universally understood and parsed) to a PHP object or array, which will not print out the way you wish. You should avoid converting it and simply do something like this:

<?php
    $string = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=...&steamids=76561198040884950");
?>

<script type="text/javascript">
        var json = <?php echo $string; ?>;
        // do things with the JSON; parse it into an object, etc
</script>