注意:尝试获取非对象的属性 [英] Notice: Trying to get property of non-object in
本文介绍了注意:尝试获取非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习一些PHP.它应该打印0,但是出现错误:
I'm learning a bit of PHP. It's supposed to print 0, however I get an error:
注意:尝试获取...中非对象的属性
Notice: Trying to get property of non-object in...
<?php
$json = '[{"assetref":"","qty":0,"raw":0}]';
$obj = json_decode($json);
print $obj->{'qty'}; // Result 0
?>
推荐答案
JSON字符串外面的括号使它成为数组内部的对象.
The brackets on the outside of your JSON string are causing it to be an object inside of an array.
您可以通过$obj[0]->{'qty'};
或更改您的json字符串,使其直接实例化为对象.
OR change your json string so it instantiates into an object directly.
$json = '{"assetref":"","qty":0,"raw":0}';
这篇关于注意:尝试获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文