通过JSON解析并显示在表中

时间:2017-12-09 22:44:35

标签: php json

这是我试图解析的JSON。

newdf.combine_first(pd.pivot_table(df,index='Date',columns='ID',values='Value',aggfunc='sum'))
Out[442]: 
          a     b
1/1/17  2.0   5.0
1/2/17  3.0  13.0
1/3/17  4.0   NaN

这是我试过的......

{
  "kickers": [
    {
      "_id": "iLntVcAmPn",
      "nflPlayerName": "Stephen Gostkowski",
      "nflPlayerNumber": 3,
      "nflPlayerPosition": "K",
      "nflPlayerTeam": "ne",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0024333"
    },
    {
      "_id": "oLe3zNIpRH",
      "nflPlayerName": "Justin Tucker",
      "nflPlayerNumber": 9,
      "nflPlayerPosition": "K",
      "nflPlayerTeam": "bal",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0029597"
    }
  ],
  "quarterbacks": [
    {
      "_id": "UXprgjbYGZ",
      "nflPlayerName": "Carson Wentz",
      "nflPlayerNumber": 11,
      "nflPlayerPosition": "QB",
      "nflPlayerTeam": "phi",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0032950"
    },
    {
      "_id": "zZVjDrLQCs",
      "nflPlayerName": "Aaron Rodgers",
      "nflPlayerNumber": 12,
      "nflPlayerPosition": "QB",
      "nflPlayerTeam": "gb",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0023459"
    }
  ],
  "widereceivers": [
    {
      "_id": "LoOT2JM8ot",
      "nflPlayerName": "Emmanuel Sanders",
      "nflPlayerNumber": 10,
      "nflPlayerPosition": "WR",
      "nflPlayerTeam": "den",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0027685"
    },
    {
      "_id": "YnA6DkyZ48",
      "nflPlayerName": "Brandin Cooks",
      "nflPlayerNumber": 14,
      "nflPlayerPosition": "WR",
      "nflPlayerTeam": "ne",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0031236"
    }
  ],
  "tightends": [
    {
      "_id": "mxrGujE01C",
      "nflPlayerName": "Jordan Reed",
      "nflPlayerNumber": 86,
      "nflPlayerPosition": "TE",
      "nflPlayerTeam": "was",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0030472"
    },
    {
      "_id": "mxrGujE01C",
      "nflPlayerName": "Jordan Reed",
      "nflPlayerNumber": 86,
      "nflPlayerPosition": "TE",
      "nflPlayerTeam": "was",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0030472"
    }
  ],
  "runningbacks": [
    {
      "_id": "u1uKHAt1n6",
      "nflPlayerName": "Adrian Peterson",
      "nflPlayerNumber": 28,
      "nflPlayerPosition": "RB",
      "nflPlayerTeam": "ne",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0021306"
    },
    {
      "_id": "0AcCT71hRi",
      "nflPlayerName": "Le'veon Bell",
      "nflPlayerNumber": 26,
      "nflPlayerPosition": "RB",
      "nflPlayerTeam": "pit",
      "nflPlayerCardType": "Common",
      "nflPlayerNFLPlayerID": "00-0030496"
    }
  ],
  "nfl_teams": [
    {
      "_id": "ari",
      "teamName": "Arizona Cardinals",
      "teamCity": "Arizona",
      "teamNameShort": "Cardinals",
      "teamAbbreviated": "ARI",
      "teamByeWeek": 8
    },
    {
      "_id": "bal",
      "teamName": "Baltimore Ravens",
      "teamCity": "Baltimore",
      "teamNameShort": "Ravens",
      "teamAbbreviated": "BAL",
      "teamByeWeek": 10
    }
  ]
}

如果您可以先指导我显示内容,然后我就可以从那里开始尝试将所有内容放入表中。这很容易是java,但我以前从未尝试过在php中这样做。这对我来说是新的!

我没有得到的问题是我得到了KICKERS数组,然后将对象放入其中。但我有几个阵列。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您不能在内部$key循环中使用相同的foreach变量。此外,代码中的$value是一个播放器对象,无法直接回显。

如果在循环中使用更多描述性变量名称将会很有帮助,这样您就可以知道它们引用的数据结构的哪个部分。

例如:

foreach ($contentsDecoded as $position => $players) { 
     foreach($players as $player) {
         echo $player->nflPlayerName; 
    }
}

或者如果你特别想要踢球者:

foreach ($contentsDecoded->kickers as $kicker {
    echo $kicker->nflPlayerName;
}