Monday, December 24, 2018

PHP json decode with number tag

Answer


Answer






Possible Duplicate:
Get a PHP object property that is a number







I tried to decode the json result from youtube data api by the following code:



 $url="http://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=jsonc";
echo "$url".'
';
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json);

$items=$json_output -> data;

$content = "{$items->content->1}";
echo $content.'
';


Everything works fine but the last two lines. Could someone please help?



And Here is the json result:




 {"apiVersion":"2.1","data":{"id":"9jDg3Dh28rE","uploaded":"2012-10-04T03:45:49.000Z",
........
"content":{"5":"http://www.youtube.com/v/9jDg3Dh28rE?version=3&f=videos&app=youtube_gdata",
"1":"rtsp://v5.cache8.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"6":"rtsp://v5.cache4.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":2403,......}}

Answer



You need to wrap the numeric property with {} to access it.



$content = $items->content->{1};



And you also don't need to use double quotes like below:



$thumbnail = "{$items->thumbnail->sqDefault}";


This should just be



$thumbnail = $items->thumbnail->sqDefault;


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...