Home  >  Article  >  Backend Development  >  Why Am I Getting the \"Notice: Trying to Get Property of Non-Object\" Error When Accessing JSON Data?

Why Am I Getting the \"Notice: Trying to Get Property of Non-Object\" Error When Accessing JSON Data?

Linda Hamilton
Linda HamiltonOriginal
2024-10-31 03:35:01452browse

Why Am I Getting the

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn