PHP json_decode not working properly

I am trying to use the PHP function json_decode to get a specific value from a json object. Sample code below:

foreach ($streams as &$i) { $chan = "http://api.justin.tv/api/stream/list.json?channel=" . $i; $json = file_get_contents($chan); //Turns the gathered file information into a string for searching purposes. echo $json . " End of json variable.<br>"; $exist = strpos($json, 'name'); // Search the file/json object for the name attribute if($exist) { // Check to see if a name existed and if so add it to the live streams and get the image. echo " <a href=\"http://justin.tv/" . $i . "\">" . $i . "</a> <br>"; $liveStreams[$count] = $i; $json_information = json_decode($json,true); $image[$count] = $json_information[0]['channel']['image_url_large']; echo "Image link should appear: " . $image[count]; $count++; } } 

So, what I'm trying to do with this, first of all, we collect which threads are active from the list presented earlier in the code. Secondly, if the stream is active, display a link to the page to view it (currently the stream is justin.tv). Currently, only live broadcasts with links to them work. I need to find out why, after decoding, I can not access the image_url_large variable. Ultimately, this will be a preview of the stream thumbnails.

I looked at various places for what was supposed to work, and even in stackoverflow I saw the following stream:

json decode in php

I tried to do this as an answer to nickf, and it still doesn't work. Any help would be greatly appreciated, and would also remain in array style instead of going into objects.

+4
source share
2 answers

Besides the stupid use of strpos (), which you seem to pretend to be someone else, it seems you just need to debug carefully.

Do something like this:

 $data = json_decode($json,true); echo "<PRE>"; var_dump($data); die(); 

Now you can see the data structure that the API provides you.

Look at the structure of the array. Note, for example, that $data['image_url_large'] does not exist. However, there is $data[0]['channel']['image_url_large'] !

Note also that instead of the stupid call to strpos (), which will give false positives if the string "name" exists anywhere in the json string, you can do something like:

 $exists = ! empty($data[0]['name']); 

EDIT Here is some code that hopefully helps you on your way:

  <?php //if you don't do this, you're flying blind. ini_set('display_errors',1); error_reporting(E_ALL); //list.json is a copy of the data from the URL you posted. $json = file_get_contents('./list.json'); //decode the data $data = json_decode($json,true); //uncomment this if you're not sure of what the json structure is. #echo "<PRE>";var_dump($data);die(); //check for the existence of a "name" key in the first item. $exist = ! empty($data[0]['name']); echo "Exist?:"; if ($exist) { echo " yes\n"; }else{ echo " no\n"; } //output the image url: echo $data[0]['channel']['image_url_large']; //say goodbye die("\n\nAll done.\n"); 

OUTPUT:

 $ php test.php Exist?: yes http://static-cdn.jtvnw.net/jtv_user_pictures/beastyqt-profile_image-c5b72ccf47b74ed2-300x300.jpeg All done. 
+5
source

Use var_dump () to check the returned json object. From your example, it looks like you need something like:

 $json_information[0]['channel']['image_url_large'] 
+2
source

All Articles