Json_decode () returning error "Note: Attempting to get a non-object property"

I am trying to write a script that receives a JSON file from a remote location (in this case twitch.tv) using cURL (I don't think this part is too relevant, although I mention it anyway). For example, one could say that a JSON object which 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 am accessing the "stream" property, I have tried the following 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 to do this, I get the error "Please note: trying to get a non-object property in D: \ Servers \ IIS \ Sites \ mysite \ getstream.php on line 48".

Am I using json_decode () incorrectly, or is there something wrong with the JSON object that I am sending from under the twitch?

Edit:

Now I have a 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 your help

+3


source to share


2 answers


You are decoding 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 like 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, the returned objects will be converted to associative arrays.

+3


source


You have set the second parameter ($ assoc) to json_decode () to true, which means it will return an associative array instead of an object. Then you tried to reference the style of the object. If you set the second parameter to true, you need to use the associative array style to access the streaming content. It will be:

$json_decoded['stream']

      

If the $ assoc parameter is set to false (or leave the parameter blank), you can refer to it as an object:



$json_decoded->stream

      

If you add var_dump to the $ json_decoded variable, you can see what it looks like. This is a good way to see what you are working with.

0


source







All Articles