我如何用 PHP 解析这个 JSON?

How can I parse this JSON with PHP?

我如何解析 this JSON,它应该显示用户在其 Steam 库存中拥有的物品。

我试过这个:

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

它returns与刚刚访问link一样。我也无法让这样的东西工作:

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

您正在回显您输入的 $data(因此您看到的与直接打开 link 相同)。要查看 json_decode 是否正常工作,您应该打印 $json。 所以而不是

echo $data;

使用

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

现在 $json 有 2 个对象。

你可以点赞

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

如果您想从 $json->rgInventory 获取所有数据;

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

等等

这是获取类型的方法

$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;
}