如何解析TwitchTV Api?

时间:2013-02-20 07:44:08

标签: php parsing

$json = file_get_contents('https://api.twitch.tv/kraken/streams');
$jsonDecoded = json_decode($json);

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($jsonDecoded));
fclose($fp);

我只需要https://api.twitch.tv中的2个字段。

这是stream-> channel-> name和streams->观众;

我如何为每个建筑创造好处?

2 个答案:

答案 0 :(得分:0)

你可以简单地迭代它并像这样复制:

$streams = array();
foreach ($jsonDecoded->streams as $stream) {
    $streams[$stream->channel->display_name] = $stream->viewers;
}

然后你就可以像使用其他键一样操作key => value php array,ie。:

foreach ($streams as $stream => $viewers) {
    echo $stream.": ".$viewers."\n";
}

答案 1 :(得分:0)

foreach ($jsonDecoded->streams as $stream) {
    $streams[$stream->channel->display_name] = $stream->viewers;
}

这对我不起作用。我不得不将其更改为此工作:

foreach ($jsonDecoded->streams as $stream) {
    $streams[$stream->channel->display_name] = $stream->viewers;
}