1

How can I parse this JSON, which is supposed to display the items a user has in their Steam inventory.

I have tried this:

$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;

It returns the same as just visiting the link. I can't get anything like this to work either:

$id = $json->type;
echo $type;
4
  • possible duplicate of Parsing JSON file with PHP Commented May 3, 2015 at 9:18
  • After json_decode you will get an array, not a object. Use $json['type'] Commented May 3, 2015 at 9:19
  • @mcklayin json_decode will return object array by default. Commented May 3, 2015 at 9:31
  • @ Kamran Adil You are right ) Commented May 3, 2015 at 9:42

3 Answers 3

1

This is how to get type

$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);

 foreach ($json->rgDescriptions as $mydata)
{
    echo $mydata->type;
}
Sign up to request clarification or add additional context in comments.

Comments

1
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;

you are echoing $data that is your input (so you see the same as opening the link directly). To see if the json_decode is working fine you should print $json. So instead of

echo $data;

use

echo '<pre>'
print_r($json);
echo '</pre>';

Comments

1
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);

Now $json has 2 objects.

you can access like.

$json->rgInventory;
$json->success;

if you want to fetch all data from $json->rgInventory;

foreach($json->rgInventory as $e){
    //var_dump($e);
    echo $e->id;
    echo $e->classid;
    echo $e->instanceid; 
}

etc.

Comments

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.