0

我的目标是通过 PHP 将 JSON 结果中的信息选择放入 MySQL。我遇到的具体问题是从 JSON 结果中访问特定信息,因为它不是简单的列表格式。这是我正在谈论的那种结构的屏幕截图,其中突出显示了我感兴趣的信息:

遗憾的是,我没有足够的声誉来发布图片,所以在这里它被重新托管在 imgur 上:

在此处输入图像描述

在 ['streams'] 下将是 100 个流的列表。我想从这些结果中的每一个中提取突出显示的信息并将其放入 MySQL 表中。

我对更简单的 JSON 文件和更简单的 json_decode 应用程序相当满意

    $result = json_decode($json);

    foreach ($result as $key => $value)

我在想也许我需要使用深度变量?但是很难找到关于它的很多信息。

如果有人可以给我任何帮助或将我指向一个好的信息来源(因为我一直在努力寻找任何东西),那么将不胜感激。

如果有更多信息有用,请告诉我,我会添加它。

编辑:链接到 json 请求:https ://api.twitch.tv/kraken/streams?limit=2/

<?php
    //import data from twitch
    $json = json_decode(file_get_contents("https://api.twitch.tv/kraken/streams?limit=100/"), true);

    //create a DB connection
    $con = mysql_connect("CONNECTION INFO :D");
    mysql_connect_db('NOPE', $con);


    $result = json_decode($json);
    foreach ($result as $key => $value) {
        if ($value) {


            mmysql_query("INSERT INTO twitchinfo (viewers, status, display_name, game, delay, name) VALUES ($value->viewers, $value->status,$value->display_name,$value->game,$value->delay,$value->name)")or die(mysql_error());
        }
        // end connection
        mysql_close($con);
    }
    ?>
4

3 回答 3

2

您的 JSON 对象基本上类似于...

[
    links: {
        self: http://example.com,
        next: http://example.com/foo,
    },
    streams: [
            {
                channel: {
                    foo: 'bar'
                },
                one: 'one',
                somethingElse: 'Something else',
                moreStuff: 'more stuff',
            }
    ]
]

当您解码 JSON 对象时,您会得到一个表示该对象的 PHP 对象/数组。

$x = json_decode('[1,2,3]') 

将为您提供与...相同的数组

$x= array(1,2,3)

如果加载显示的 JSON 并运行它:

foreach ($result as $key => $value)

这与访问$result->links和相同$result->streams。每个都包含更多数据。

如果我想在我的示例中从频道中获取“foo”元素,我会这样做:

$streams = $result->streams //Get the streams array
$stream = $streams[0]       //Get the first stream object
$channel = $stream->channel //Get the channel object
$foo = $channel->foo        //Get the value 'bar' out of the foo property.

只要结构是可预测的(而且确实如此),我就可以遍历流,因为它只是一个数组。

$streams = $result->streams //Get the streams array
foreach ($streams as $stream) {
    $channel = $stream->channel //Get the channel object
    $foo = $channel->foo        //Get the value 'bar' out of the foo property of every stream.
}
于 2013-11-07T13:54:53.080 回答
2

在浏览器中打开此网址 -

https://api.twitch.tv/kraken/streams?limit=100/

给我一个 JSON。我把它复制到——

http://www.jsoneditoronline.org/

并看到它有_links键。

根据您的问题,试试这个 -

$result = json_decode($json);
if( isset( $result['streams'] )){
    $streams = $result['streams'];
    foreach($streams as $stream) {
        $viewers = $stream->viewers;
        $status = $stream->channel->status;
        $display_name = $stream->channel->display_name;
        $game = $stream->channel->game;
        $delay = $stream->channel->delay;
        $name = $stream->channel->name;
        $sql = "INSERT INTO twitchinfo (viewers, status, display_name, game, delay, name) VALUES ($viewers, \"$status\", \"$display_name\", \"$game\", $delay, \"$name\")";
        mysql_query($sql)or die(mysql_error());         
    }
}
于 2013-11-07T13:57:05.177 回答
0

也许你可以这样做:

$values = array();
$result = json_decode($json);

foreach($result['streams'] as $stream) {
    array_push(
        $values,
        array(
            'viewers'      => $stream['viewers'],
            'status'       => $stream['channel']['status'],
            'display_name' => $stream['channel']['display_name'],
            'game'         => $stream['channel']['game'],
            'delay'        => $stream['channel']['delay'],
            'name'         => $stream['channel']['name'],
        )
    );
}

或者:

foreach($result['streams'] as $stream) {
    $sqlQuery= "INSERT INTO TABLE(viewers, status, display_name, game, delay, name) VALUES ($stream['viewers'], $stream['channel']['status'], $stream['channel']['display_name'], $stream['channel']['game'], $stream['channel']['delay'], $stream['channel']['name']);"
    //dbHandler->executeQuery($sqlQuery);
}
于 2013-11-07T13:48:19.870 回答