【问题标题】:Json string readingJson字符串读取
【发布时间】:2015-12-17 14:08:28
【问题描述】:

回答: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 ) ) ) ) 

【问题讨论】:

  • 您能否为我们提供print_r($myDecodedStringFromJSon)。但据我猜测,你的字符串被解码为数组$myDecodedStringFromJSon['playerStatsSummaries']
  • @George-Cristian 是的,您只需要 json_decode(string, true);这样就把它变成了一个数组。

标签: php json decode


【解决方案1】:

只需解码 JSON 并使用 foreach 循环。下面的代码会对你有所帮助。

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

【讨论】:

    猜你喜欢
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2011-07-17
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多