从JSON API获取数据时出错

时间:2014-12-16 07:52:16

标签: php arrays json

我是PHP的新手。

目前,我想从JSON API网址获取数据:https://kickass.so/json.php?q=test+category:tv&field=seeders&order=desc&page=2

我尝试了以下内容:

<?php
  $query = file_get_contents('https://kickass.so/json.php?q=test+category:tv&field=seeders&order=desc&page=2');
  $parsed_json = json_decode($query, true);
  foreach ($parsed_json as $key => $value)
  {
    echo $value['title'];
  }
?>

我想在这里获得价值:

标题,类别,链接,哈希等。

但是我收到了这个错误:

Warning: Illegal string offset 'title' in C:\xampp\htdocs\xxxxx\test.php on line 6
K
Warning: Illegal string offset 'title' in C:\xampp\htdocs\xxxxx\test.php on line 6
h
Warning: Illegal string offset 'title' in C:\xampp\htdocs\xxxxx\test.php on line 6
B
Warning: Illegal string offset 'title' in C:\xampp\htdocs\xxxxx\test.php on line 6
e
Warning: Illegal string offset 'title' in C:\xampp\htdocs\xxxxx\test.php on line 6
1
Notice: Undefined index: title in C:\xampp\htdocs\xxxxx\test.php on line 6

2 个答案:

答案 0 :(得分:3)

它仍在数组list中:

[title] => Kickasstorrents test category:tv
    [link] => http://kickass.so
    [description] => BitTorrent Search: test category:tv
    [language] => en-us
    [ttl] => 60
    [total_results] => 1002
    [list] => Array // <------- Another nesting
        (
            [0] => Array
                (
                    [title] => The Simpsons S24E10 A Test Before Trying 480p WEB-DL x264-mSD
                    [category] => TV
                    [link] => http://kickass.so/the-simpsons-s24e10-a-test-before-trying-480p-web-dl-x264-msd-t7006138.html
                    [guid] => http://kickass.so/the-simpsons-s24e10-a-test-before-trying-480p-web-dl-x264-msd-t7006138.html
                    [pubDate] => Sunday 20 Jan 2013 10:57:02 +0000
                    [torrentLink] => http://torcache.net/torrent/0D616E1F4578942883F9A0BF676DCDDA02B1A894.torrent?title=[kickass.so]the.simpsons.s24e10.a.test.before.trying.480p.web.dl.x264.msd
                    [files] => 4
                    [comments] => 0
                    ....
                )

您可以将其直接指向循环内部:

foreach ($parsed_json['list'] as $key => $value)
{
    echo $value['title'];
}

答案 1 :(得分:1)

首先,您需要了解JSON对象的结构。如果你创建一个$ parsed_json的print_r,那么结果如下:

Array
(
    [title] => Kic ....
    [link] => http://kick...
    [description] => BitT...
    [language] => en-us
    [ttl] => 60
    [total_results] => 1002
    [list] => Array
        (
            [0] => Array
                (
                    [title] => The ...
                    [category] => TV
                    [link] => http://kicka...
                    [guid] => http://kicka...
                    [pubDate] => Sunday 20 Jan 2013 10:57:02 +0000
                    [torrentLink] => http://torcac...
                    [files] => 4
                    [comments] => 0
                    [hash] => 0D616...
                    [peers] => 18
                    [seeds] => 17
                    [leechs] => 1
                    [size] => 13652...
                    [votes] => 3
                    [verified] => 1
                )

            [1] => Array
                (
                    [title] => Lio..
                    [category] => TV
                    [link] => http://kicka...
                    [guid] => http://kick...
                    [pubDate] => Sunday 7 Jul 2013 01:45:16 +0000
                    [torrentLink] => http://tor...
                    [files] => 2
                    [comments] => 7
                    [hash] => BBF5D...
                    [peers] => 17
                    [seeds] => 17
                    [leechs] => 0
                    [size] => 1614025889
                    [votes] => 8
                    [verified] => 0
                )
                ...

如您所见,此对象只包含一个寄存器,该寄存器包含另一个名为“list”的寄存器,其中包含您要查找的数组。

此代码将循环没有问题:

$parsed_json = json_decode($query, true);


if( !isset( $parsed_json['list'])) die("Invalid format");

foreach ($parsed_json['list'] as $key => $value)
{
    echo $value['title'];
}