json_decode() returning error "Notice: Trying to get property of non-object"
Asked Answered
W

2

8

I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:

$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}

I access the "stream" property, I have tried the follow code:

<?php
    $json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}

    $json_decoded = json_decode($json_object, true);
    echo $json_decoded->stream;
?>

When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".

Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?

Edit:

I now have the JSON object:

{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}

If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?

Thanks in advance for any help

Waldgrave answered 8/8, 2014 at 1:59 Comment(3)
Remove second argument from json_decode.Artwork
That json_decode($jsonString) will return a stdClass, but it will return a array when added a true argument.Cerell
wow, can't believe I didn't catch that. I though that parameter was for associative things, guess that only applies to arrays. Mind posting that as an answer so I can mark it as such?Waldgrave
S
7

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.

Samora answered 8/8, 2014 at 2:8 Comment(9)
I now have the JSON object {"access_token":"qwerty12345","refresh_token":"asdfghjk=","scope":["user_read"]}. If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?Waldgrave
That's not valid jsonSamora
Any reason why you wanted to use it as a class instead of array?Samora
You are right about it being invalid json, somehow it got messed up when I pasted it here. This json appears to be proper according to online validators: {"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}Waldgrave
I have no preference over array vs. class, nor do I fully understand the difference in this case. All I need to do is read the properties for use later, something like: $object->access_token.Waldgrave
This still works for me, not to sure where it's going wrong for you. With the JSON you just posted here, run it as $data = json_decode($json_str, true); you can then access the access_token like $data['access_token']Samora
Awesome, that works for me too now. For future reference for myself, how could I make the json object into a class instead of an array, and access it with the arrow notation I mentioned above?Waldgrave
See the second example in my answer. Playing with Std Classes are a nightmare thoughSamora
alright, thanks! Not sure why this wasn't working for me before, but everything seems to do what I'd like it to do now. I'll stick with your advice though and keep everything as an array :)Waldgrave
H
4

You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:

$json_decoded['stream']

If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:

$json_decoded->stream

If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.

Humanitarianism answered 8/8, 2014 at 2:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.