无法将json解码为php输出

时间:2014-02-24 22:46:38

标签: php json api

我正在尝试解码json并使用php显示结果,但什么都没有回来。谁能看一看并告诉我我做错了什么?

这是我的一个例子:

class League

{
public function __construct($region, $id, $summid)  {
    global $apiKey;
    global $errorAPI;
    $data = RIOT_API . "$region/v2.3/league/by-summoner/" . urlencode($summid) . "?api_key=$apiKey";
    $info = httpResp($data);
    if ($info[1] == false)
        {
        $League = json_decode(file_get_contents($data) , true);
        for ($i = 0; $i < count($League['entries']); $i++)
            {
            if ($League['entries'][$i]['playerOrTeamId'] == $summid)
                {
                $floor = $i;
                break;
                }
            }

        $this->leaguename = $League['entries'][$floor]['leagueName'];
        $this->tier = $League['entries'][$floor]['tier'];
        $this->points = $League['entries'][$floor]['leaguePoints'];
        $this->rank = $League['entries'][$floor]['rank'];
        $this->wins = $League['entries'][$floor]['wins'];
        $this->exists = true;
        }
      else
        {
        echo "Error " . $info[0] . " - " . array_search($info[0], $errorAPI);
        $this->exists = false;
        }
    }
}

这是我输出的php:

$newLeague1 = new League("euw", "Deew0n", "24361317");

echo '<br />League Name : ', $newLeague1->name;
echo "\n";
echo '<br />Position : ', $newLeague1->position;
echo "\n";
echo '<br />League Points : ', $newLeague1->leaguePoints;
echo "\n";
echo '<br />Wins/Losses: ', $newLeague1->wins, "/", $newLeague1->totalgames;
echo "\n";
echo '<br />Division Name: ', $newLeague1->tier, ' ', $newLeague1->rank;

最后这是我正在处理的json:http://pastebin.com/9FKgrjeS

1 个答案:

答案 0 :(得分:1)

你的JSON是:

[
    {
        "name": "Jayce's Emmissaries",
        "tier": "SILVER",
        "queue": "RANKED_SOLO_5x5",
        "entries": [ {...}, {...} ]
        "participantId": "24361317"
    }
]

...这是一个包含1个项目的数组:一个包含5个项目的对象。

如果您可以更改JSON,请删除外部[],否则请更改代码以访问此第[0]条。