通过PHP向MySQL复杂的JSON数组

时间:2013-11-07 13:29:28

标签: php mysql json api

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

我遗憾地没有足够的声誉来发布图片所以在这里重新托管图片:

enter image description here

在['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);
    }
    ?>

3 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分: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());         
    }
}

答案 2 :(得分: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);
}