I am working with following APIs:
http://api.steampowered.com/ISteamApps/GetAppList/v0002/?format=json
https://store.steampowered.com/api/appdetails?appids=GAMEID
(gameid example: "730" - counter strike)
My goal with both of these is to search them for data. For example I want the first API to give me a name of a game based on it's ID and the second one to give me specific information about a game for example if it has trading cards (id:29 in the API).
I tried a few things but I am kinda lost on this beacuse I don't really understand JSON so I would really appreciate some help.
I am open to both PHP and JS solutions.
IN PHP, you can use the json_decode() function to convert JSON data into an array. You can them access the values as you do for a classic array :
$appID = 730 ;
$url = 'https://store.steampowered.com/api/appdetails?appids=' . $appID ;
$content = file_get_contents($url) ; // retrieve the JSON data string from the website
$data = json_decode($content, true); // convert the JSON string into PHP array
echo $data[$appID]['data']['name'] ; // Counter-Strike: Global Offensive
var_dump($data[$appID]['data']['is_free']); // bool(true)
Im using MySQL to obtain data into $username and $chance.
There are two usernames in the data but it only loads the first one.
var data = [
{
"name" : "<?php echo $username; ?>",
"hvalue" : <?php echo $chance; ?>
},
];
To give a correct answer, we'll have to know what your variables $username and $chance exactly look like.
If you only have a string there, then only one value is possible.
If these variables are php arrays, you'll have to use json_encode to make a JSON array from them, before you can add them to your javascript object. (see https://secure.php.net/manual/en/function.json-encode.php)
Anyway, the best way to send a PHP array to JS would be to create the whole array in PHP and then encode it to JSON in order to use all the values in JS.
var data = <?php echo json_encode($data); ?>
More advanced: use an ajax request to avoid mixing up PHP an JS. But that's another story.
Javascript:
$.getJSON("path", function(data){alert(data.name)})
Guys in this line "data" is not working as an object. name is unrecognizable here.
I am sending multiple outputs in php using json_encode() function but not able to get those values in javascript file.
PHP Code:
$book = array("name"=>"bob", "num"=>1);
echo json_encode($book);
I have a JSON formed #Javascript and passed to PHP via AJAX , problem is that I cannot assign keys to each JSON Object inside the JSON Array .
The JSON Array looks something like this :
[{"jobId":"90","cname":"Subhasish","removal_id":101,"quantity":"3"},{"jobId":"90","cname":"Subhasish","removal_id":102,"quantity":"2"},{"jobId":"90","cname":"Subhasish","removal_id":103,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":104,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":105,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":106,"quantity":"5"},{"jobId":"90","cname":"Subhasish","removal_id":107,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":108,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":109,"quantity":"4"}]
,Now , in PHP ,how to iterate the data , I am a newbie to PHP , so please excuse me if I am asking a very silly question.
The link you provided has examples with key, mine is without key.
Thanks
$json = '[{"jobId":"90","cname":"Subhasish","removal_id":101,"quantity":"3"},{"jobId":"90","cname":"Subhasish","removal_id":102,"quantity":"2"},{"jobId":"90","cname":"Subhasish","removal_id":103,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":104,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":105,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":106,"quantity":"5"},{"jobId":"90","cname":"Subhasish","removal_id":107,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":108,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":109,"quantity":"4"}]';
$array = json_decode($json,true);
foreach($array as $item) {
echo $item['jobId']."-".$item['cname']."-".$item['removal_id']."-".$item['quantity']."\n";
}
Output
90-Subhasish-101-3
90-Subhasish-102-2
90-Subhasish-103-4
90-Subhasish-104-4
90-Subhasish-105-0
90-Subhasish-106-5
90-Subhasish-107-0
90-Subhasish-108-0
90-Subhasish-109-4
Example
Using json_decode($json_object).
$json = json_decode($json_object);
foreach($json as $j){
print_r($j)
}
I have an array of file paths, which is stored in a PHP array called $files, which I would like to pass to a javascript array using this command:
var files = <?php echo json_encode($files)?>;
Which is fine, however it doesn't pass any values which have special characters for example one that contains 'æ'. Instead that value in Javascript comes out as null. Is there another way of preserving these characters? The values are all file paths, which seem to be passed over OK using json_encode, just not these special characters.
I've solved it. I essentially ran through the array using url_encode, and THEN JSON'd that for Javascript to interpret.
$filesEncoded = array();
foreach ($files as $f) {
array_push($filesEncoded,urlencode($f));
}
<script>
var files = <?php echo json_encode($filesEncoded)?>;
</script>