解码JSON(PHP)

时间:2016-05-17 22:13:21

标签: php arrays json steam steam-web-api

如何选择“total_items”和“average_price”的数量?

JSON:

   {
  "status" : "success",
  "data" : {
    "items" : [
      {
        "market_hash_name" : "AK-47 | Redline (Field-Tested)",
        "total_items" : 698,
        "lowest_price" : "3.90",
        "highest_price" : "300.00",
        "cumulative_price" : "4669.62",
        "recent_sales_info" : {
          "hours" : "17.94",
          "average_price" : "4.23"
        }
      }
    ]
  }
}

这就是我的PHP脚本:

$link = 'skin.json';
    $string = file_get_contents($link);
    $obj = json_decode($string, TRUE);
$name = $obj['items']['market_hash_name'];
$itmes = $name['total_items'];
$itmes = $name['average_price'];

如何在变量中保存“total_items”和“average_price”的数量? 谢谢 问候。 恩格

2 个答案:

答案 0 :(得分:1)

就像安德烈亚斯写的那样,或者:

// first, new variable (shorter lines/less depth afterwards)
$article = $obj['data']['items'][0];

// then 
$total_items = $article['total_items'];
$avg_price = $article['recent_sales_info']['average_price'];

Enge,请注意“项目”: [是JSON对象中的一个开放数组,这是你错过的零...

答案 1 :(得分:0)

试试这个:

arr.filter(item => item.fields.some(deepItem => deepItem.label === 'rar')); // [{...}, ...]