1

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.

0

2 Answers 2

3

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...
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you very much. You are much too kind. A little bit new to this, even the easiest of tutorials (the duplicate mark, for instance) are too confusing.
You're very welcome @JohnSmith. You'll get there! persistence is the key :)
If my answer helped you, please mark it as the correct answer, thank you !
Of course, but can't do it instantly, waiting for a timer!
@JohnSmith Mark this as the answer. Don't hold him hostage while you ask completely irrelevant, random questions in the comments section.
|
2
<?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);
}

3 Comments

Thank you, I think I get the concept now! While I'm typing I might aswell ask, how do I structure this API so it's readable? It's just one long series of text looking in the browser..
depending on your editor, you may have built-in features or plugins available to auto-format JSON. I use Notepad++ and there is a plugin called "JSON Viewer" that auto-formats for me.
Perfect, just what I'm looking for. This community is too scrub-friendly. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.