Reputation: 477
Okay, so I think you get what I wanna do by just watching the code.
//Get JSON text file (Steam API)
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');
//Decode JSON
$game_json = json_decode($json, true);
//Target game name and echo it
echo $game_json['name'];
The JSON itself comes in this order (unstructured, very sorry):
{"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"
So my target is ""name":"Tropico 4: Steam Special Edition""
, which is what I want to echo on my page. I'm not sure if it helps, but "name":
appears once, is something like [0]
needed in my code to target the first? Is the nesting what's stopping me here, or is the $game_json['name'];
an incorrect way of targeting?
ANY tips and/or help will be much appreciated. Thanks.
Upvotes: 1
Views: 1611
Reputation: 293
<?php
//This is your json string
$string = {"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"...
//Turn JSON string into object
$data = json_decode($string);
//Turn your object into an array (easier to work with in this case)
$data = (Array)$data;
//Get name of item with "57690" key
$name = $data["57690"]->data->name;
//Echo the name
echo($name);
//You can also echo out all names of all items like this:
foreach($data as $key => $item)
{
echo($item->data->name);
}
Upvotes: 2
Reputation: 98991
In the future, use print_r($game_json)
to check the array
structure.
<?php
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');
$game_json = json_decode($json, true);
echo $game_json['57690']['data']['name'];
//Tropico 4: Steam Special Edition
echo $game_json['57690']['data']['required_age'];
//0
//etc...
Upvotes: 3