Json字符串阅读

时间:2015-12-17 14:08:28

标签: php json decode

答案:PHP/JSON - stdClass Object

我试图从像这样的json读取一些统计数据,但我在某个地方犯了一个错误。 我尝试使用foreach功能,但我不知道错误在哪里:

foreach($myDecodedStringFromJSon->playerStatsSummaries as $stats){
 // Do some here
}

这是我的JSON:

{
   "summonerId":39656713,
   "playerStatSummaries":[
      {
         "playerStatSummaryType":"CAP5x5",
         "wins":0,
         "modifyDate":1431942577000,
         "aggregatedStats":{
            "totalChampionKills":3,
            "totalMinionKills":79,
            "totalTurretsKilled":0,
            "totalNeutralMinionsKilled":0,
            "totalAssists":4
         }
      },
      {
         "playerStatSummaryType":"CoopVsAI",
         "wins":22,
         "modifyDate":1431942577000,
         "aggregatedStats":{
            "totalChampionKills":73,
            "totalMinionKills":2344,
            "totalTurretsKilled":24,
            "totalNeutralMinionsKilled":0,
            "totalAssists":179
         }
      },
      {
         "playerStatSummaryType":"OdinUnranked",
         "wins":0,
         "modifyDate":1431942577000,
         "aggregatedStats":{

         }
      },
      {
         "playerStatSummaryType":"Unranked",
         "wins":18,
         "modifyDate":1431942577000,
         "aggregatedStats":{
            "totalChampionKills":63,
            "totalMinionKills":1789,
            "totalTurretsKilled":14,
            "totalNeutralMinionsKilled":5,
            "totalAssists":181
         }
      },
      {
         "playerStatSummaryType":"Unranked3x3",
         "wins":0,
         "modifyDate":1431942577000,
         "aggregatedStats":{

         }
      },
      {
         "playerStatSummaryType":"AramUnranked5x5",
         "wins":38,
         "modifyDate":1445430660000,
         "aggregatedStats":{
            "totalChampionKills":414,
            "totalTurretsKilled":28,
            "totalAssists":1556
         }
      }
   ]
}

编辑:

stdClass Object ( [summonerId] => 39656713 [playerStatSummaries] => Array ( [0] => stdClass Object ( [playerStatSummaryType] => CAP5x5 [wins] => 0 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 3 [totalMinionKills] => 79 [totalTurretsKilled] => 0 [totalNeutralMinionsKilled] => 0 [totalAssists] => 4 ) ) [1] => stdClass Object ( [playerStatSummaryType] => CoopVsAI [wins] => 22 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 73 [totalMinionKills] => 2344 [totalTurretsKilled] => 24 [totalNeutralMinionsKilled] => 0 [totalAssists] => 179 ) ) [2] => stdClass Object ( [playerStatSummaryType] => OdinUnranked [wins] => 0 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( ) ) [3] => stdClass Object ( [playerStatSummaryType] => Unranked [wins] => 18 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 63 [totalMinionKills] => 1789 [totalTurretsKilled] => 14 [totalNeutralMinionsKilled] => 5 [totalAssists] => 181 ) ) [4] => stdClass Object ( [playerStatSummaryType] => Unranked3x3 [wins] => 0 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( ) ) [5] => stdClass Object ( [playerStatSummaryType] => AramUnranked5x5 [wins] => 38 [modifyDate] => 1445430660000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 414 [totalTurretsKilled] => 28 [totalAssists] => 1556 ) ) ) ) 

1 个答案:

答案 0 :(得分:0)

使用foreach解码JSON并循环。以下代码将为您提供帮助。

$result = json_decode($json);
  foreach ($result->playerStatSummaries as $stats) {
  echo 'Wins: ' . $stats->wins;
  echo '<br>';
}