无法从解析的JSON输出中获取对象

时间:2016-10-07 15:41:40

标签: php arrays json

我正在使用twitter API,我试图解析json输出。但不知怎的,我做错了什么,而且我没有得到我想要的东西。

在这种情况下,我用PHP写作。开始通过数组循环并且screen_name和文本正在相应地工作。

foreach ($statuses as $obj) {

  $screen_name  =   filter_var($obj->user->screen_name, FILTER_SANITIZE_STRING);
  $text         =   filter_var($obj->text, FILTER_SANITIZE_STRING);
  $urls         =   filter_var($obj->entities->urls->url, FILTER_SANITIZE_URL);
}

JSON数组输出:

array (
  0 => 
  stdClass::__set_state(array(
     'created_at' => 'Fri Oct 07 15:31:01 +0000 2016',
     'text' => 'test',
     'entities' => 
    stdClass::__set_state(array(
       'hashtags' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'text' => '30Under30',
           'indices' => 
          array (
            0 => 36,
            1 => 46,
          ),
        )),
      ),
       'symbols' => 
      array (
      ),
       'user_mentions' => 
      array (
      ),
       'urls' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'url' => 'https://Forbes.com',
           'expanded_url' => 'https://twitter.com/i/web/status/784415744427687936',
           'display_url' => 'twitter.com/i/web/status/7…',
           'indices' => 
          array (
            0 => 117,
            1 => 140,
          ),
        )),
      ),
    )),
     'source' => 'Sprinklr',
     'in_reply_to_screen_name' => NULL,
     'user' => 
    stdClass::__set_state(array(
       'id' => 91478624,
       'screen_name' => 'test',
       'url' => 'http://Forbes.com',
       'entities' => 
      stdClass::__set_state(array(
         'url' => 
        stdClass::__set_state(array(
           'urls' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'url' => 'http://Forbes.com',
               'expanded_url' => 'http://forbes.com',
               'display_url' => 'forbes.com',
               'indices' => 
              array (
                0 => 0,
                1 => 22,
              ),
            )),
          ),
        )),
         'description' => 
        stdClass::__set_state(array(
           'urls' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'url' => 'http://Forbes.com',
               'expanded_url' => 'http://Forbes.com',
               'display_url' => 'Forbes.com',
               'indices' => 
              array (
                0 => 28,
                1 => 48,
              ),
            )),
          ),
        )),
      )),
       'protected' => false,
       'notifications' => false,
    )),
     'geo' => NULL,
     'lang' => 'en',
  )),
)

1 个答案:

答案 0 :(得分:0)

由于urls是一个数组,因此需要对其进行索引。

$urls         =   filter_var($obj->entities->urls[0]->url, FILTER_SANITIZE_URL);