php json_decode() 返回错误“注意:试图获取非对象的属性”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25195010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json_decode() returning error "Notice: Trying to get property of non-object"
提问by grahamhoyes
I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:
我正在尝试编写一个脚本,该脚本使用 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}
I access the "stream" property, I have tried the follow code:
我访问“流”属性,我尝试了以下代码:
<?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;
?>
When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".
当我尝试此操作时,出现错误“注意:尝试在第 48 行获取 D:\Servers\IIS\Sites\mysite\getstream.php 中非对象的属性”。
Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?
我是否使用了 json_decode() 错误,或者我从 twitch 发送的 JSON 对象有问题吗?
Edit:
编辑:
I now have the JSON object:
我现在有 JSON 对象:
{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
If I try to decode it using json_decode()
I get the following error: Object of class stdClass could not be converted to string
. Any advice?
如果我尝试使用解码它,json_decode()
我会收到以下错误:Object of class stdClass could not be converted to string
. 有什么建议吗?
Thanks in advance for any help
在此先感谢您的帮助
回答by Bankzilla
You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array
您正在将 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
If you remove the second parameter and run it as json_decode($json_object)
如果删除第二个参数并将其作为 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
See the documentation, When TRUE, returned objects will be converted into associative arrays.
请参阅文档,当为 TRUE 时,返回的对象将转换为关联数组。
回答by johnjg12
You have set the second parameter ($assoc) of json_decode()to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:
您已将json_decode()的第二个参数 ($assoc) 设置为 true,这意味着它将返回关联数组而不是对象。然后您尝试引用对象样式。如果将第二个参数设置为 true,则需要使用关联数组样式来访问流内容。这将是:
$json_decoded['stream']
If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:
如果将 $assoc 参数设置为 false(或不指定参数),则可以将其作为对象引用:
$json_decoded->stream
If you do var_dumpon the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.
如果您对 $json_decoded 变量执行var_dump,您将看到它的样子。这是查看您正在使用的内容的好方法。