简体   繁体   中英

Notice: Trying to get property of non-object in

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 

?>

The brackets on the outside of your JSON string are causing it to be an object inside of an array.

You can access the object by specifying which array member you want with $obj[0]->{'qty'};

OR change your json string so it instantiates into an object directly.

$json = '{"assetref":"","qty":0,"raw":0}';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM