JSON_Decode() Returning Error "Notice: Trying to Get Property of Non-Object"

json_decode() returning error Notice: Trying to get property of non-object

You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array

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)

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.

PHP JSON trying to get property of non-object error when looping

As I have said already in the comments above, once you've set your json_decode flag as true, in turn, you'll get an array instead of an object.

Following in the comments:

Can you show me $json_output array ? – Prakash

@Prakash, array (size=1) 'id' => string 'xYue78ee9es' (length=10) – user123451

It seems its just a normal flat array, then treat it as such. Actually, you wouldn't need a foreach anymore. You can just access individual elements directly:

echo $json_output['id'];
echo $json_output['username'];

If you still would like to use a foreach, then it'll just traverse the first level, so you wouldn't need to put any index anymore:

foreach($json_output as $output) {
echo $output;
// no need for that ['id'] or ->id anymore
// its just strings on $output
}

If you'd like to restrict some elements from getting echoed inside the foreach loop, simple if statement testing the keys should suffice:

foreach($json_output as $key => $output) {
if($key === 'id') {
echo $output; // echoes only id
}
}

Trying to get property of 'non-object' - json_decode from API

By that API response you don't need to read

$responseNode["Value"] = $sdata->answer;

instead of that you need to read

$responseNode["Value"] = $sdata[0]->response->answer;

because answer is nested under response...

Hint: just do this

$data = json_decode('[{"response":{"answer":"Good morning.","question":"hello world","userid":"1234567890"}},200]');
print_r($data);

and output will be:

Array
(
[0] => stdClass Object
(
[response] => stdClass Object
(
[answer] => Good morning.
[question] => hello world
[userid] => 1234567890
)

)

[1] => 200
)

Retrieve json data. but having this error - Trying to get property of non object

You should use like this:

<?php
$jsonData = '{
"id": 1,
"name": "Jasper Mendiola",
"username": "jasperduane77",
"created_at": "2017-07-22 10:11:04",
"updated_at": "2017-07-22 10:11:04",
"userType": "Administrator",
"user_details": {
"user_id": 1,
"profile_pic": null,
"bio": "frustrated software blahblahblah"
}
}';
$josnObj = json_decode($jsonData);
echo $josnObj->user_details->bio;//frustrated software blahblahblah
?>

Trying to get property of non-object in json from API

You should use avatar_url like this:

<td><img src="{{ $player['avatar_url'] }}">{{ $player['username'] }}</td>

because what you pass to view is an array, not an object.



Related Topics



Leave a reply



Submit