Home > Article > Backend Development > Why Am I Getting the \"Notice: Trying to Get Property of Non-Object\" Error When Accessing JSON Data?
Understanding the "Notice: Trying to Get Property of Non-Object" Error
In your code, you're trying to get the value of the player_name property from the $pjs variable, which is a JSON-decoded object. However, the error message indicates that you're trying to access a property of a non-object.
Analyzing the Issue
Looking at the var_dump output of $pjs, you can see that it's an array containing a single object. To access the object's properties, you need to access the array element first.
Resolving the Issue
To fix the error, you can use the following modified code:
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson'); $pjs = json_decode($js); echo $pjs[0]->player_name;
By accessing the array element at index 0, you can access the object and get the value of the player_name property.
The above is the detailed content of Why Am I Getting the \"Notice: Trying to Get Property of Non-Object\" Error When Accessing JSON Data?. For more information, please follow other related articles on the PHP Chinese website!