用PHP打印JSON

用PHP打印JSON,php,json,Php,Json,屏幕上没有显示任何内容,下面的代码是否有效?我知道在接收到的数据中有一个名为“text”的JSON参数,但不知道如何打印出来 <?php $url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline //print_r(get_data($url)); //dumps the content, you can manipu

屏幕上没有显示任何内容,下面的代码是否有效?我知道在接收到的数据中有一个名为“text”的JSON参数,但不知道如何打印出来

    <?php
    $url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
    //print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
    $obj = json_decode($data);
    print $obj->{'text'};
    /* gets the data from a URL */

    function get_data($url)
    {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }
    ?>

您应该将get_data返回的值分配给一个变量,并将其传递给json_decode,即:

<?php
    $url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
    //print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
    $data = get_data($url);
    $obj = json_decode($data);
    print $obj->text;
    /* gets the data from a URL */

    function get_data($url)
    {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }
    ?>

您应该将get_data返回的值分配给一个变量,并将其传递给json_decode,即:

<?php
    $url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
    //print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
    $data = get_data($url);
    $obj = json_decode($data);
    print $obj->text;
    /* gets the data from a URL */

    function get_data($url)
    {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }
    ?>
$data未设置,您不需要大括号

$data未设置,您不需要大括号

这应该有效:

$obj = json_decode(get_data($url));
$text = $obj[0]->text;
遇到这样的问题时,最好尝试使用var_dump$obj之类的方法。执行此操作后,立即可以清楚地看到$obj[0]->文本是您要查找的内容

@benhowdle89评论:

foreach ($obj as $item) {
    $text = $item->text;
}
这应该起作用:

$obj = json_decode(get_data($url));
$text = $obj[0]->text;
遇到这样的问题时,最好尝试使用var_dump$obj之类的方法。执行此操作后,立即可以清楚地看到$obj[0]->文本是您要查找的内容

@benhowdle89评论:

foreach ($obj as $item) {
    $text = $item->text;
}

打印r$obj以查看实际结构。快速浏览一下,整个json都包装在一个数组中,因此它可能是$obj[0]->文本。从$obj包含的内容开始,然后从那里开始。打印$obj以查看实际结构。快速看一眼,整个json被包装在一个数组中,因此它可能是$obj[0]->文本。从$obj包含的内容开始,然后从那里开始。这就是一个,谢谢mfonda,我如何将它放入一个循环中,以便它打印出所有的“文本”?再次感谢!帮我把那一本拿出来,谢谢mfonda,我怎样才能把它放进一个循环,这样它就能打印出所有的“文本”?再次感谢!帮我卸了很多东西