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;
Mitch8910
  • 185
  • 1
  • 2
  • 15

3 Answers3

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>';
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
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.

Manish Shukla
  • 1,355
  • 2
  • 8
  • 21
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;
}
Kamran
  • 2,711
  • 2
  • 17
  • 24