doude1917 2014-02-24 22:46
浏览 29

无法将json解码为php输出

I'm trying to decode json and display results with php but getting nothing back. Could anyone take a look and tell me what I'm doing wrong?

Here is an example of my 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;
        }
    }
}

Here is my php for outputting:

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

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

And lastly this is the json I am dealing with : http://pastebin.com/9FKgrjeS

  • 写回答

1条回答 默认 最新

  • drtppp75155 2014-02-24 23:40
    关注

    Your JSON is:

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

    ...which is an array with 1 item: an object with 5 items.

    If you can change the JSON, remove the outer [], otherwise change the code to access this [0]th entry.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程