json_decode()返回错误"注意:尝试获取非对象的属性"

gra*_*yes 5 php json twitch

我正在尝试使用cURL编写一个从远程位置(在本例中为twitch.tv)获取JSON文件的脚本(不要认为该部分太相关,不过我最好还是提一下).例如,假设它返回的JSON对象在存储在变量中之后看起来像这样:

$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
Run Code Online (Sandbox Code Playgroud)

我访问"stream"属性,我试过以下代码:

<?php
    $json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}

    $json_decoded = json_decode($json_object, true);
    echo $json_decoded->stream;
?>
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,我收到错误"注意:尝试在第48行的D:\ Servers\IIS\Sites\mysite\getstream.php中获取非对象的属性".

我是否使用json_decode()错误,或者我是从抽搐发送的JSON对象有什么问题?

编辑:

我现在有了JSON对象:

{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用它解码它json_decode()我得到以下错误:Object of class stdClass could not be converted to string.有什么建议?

在此先感谢您的帮助

Ban*_*lla 6

您正在将JSON解码为数组.json_decode($json_object, true); 将返回一个数组

array (size=2)
  '_links' => 
    array (size=2)
      'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
      'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
  'stream' => null
Run Code Online (Sandbox Code Playgroud)

如果删除第二个参数并将其运行为 json_decode($json_object)

object(stdClass)[1]
  public '_links' => 
    object(stdClass)[2]
      public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
      public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
  public 'stream' => null
Run Code Online (Sandbox Code Playgroud)

请参阅文档,如果为TRUE,则返回的对象将转换为关联数组.