Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: JSON decoding problem

Tags:

json

php

<?php
$handle = fopen("https://graph.facebook.com/[email protected]&type=user&access_token=2227472222|2.mLWDqcUsekDYK_FQQXYnHw__.3600.1279803900-100001310000000|YxS1eGhjx2rpNYzzzzzzzLrfb5hMc.", "rb");
$json = stream_get_contents($handle);
fclose($handle);
echo $json;
$obj = json_decode($json);
print $obj->{'id'};
?>

Here is the JSON: {"data":[{"name":"Sinan \u00d6zcan","id":"610914868"}]}

It echos the JSON but I was unable to print the id.

Also I tried:

<?php
$obj = json_decode($json);
$obj = $obj->{'data'};
print $obj->{'id'};
?>
like image 834
ilhan Avatar asked Dec 12 '22 20:12

ilhan


1 Answers

Note that there is an array in the JSON.

{
    "data": [   // <--
      {
        "name": "Sinan \u00d6zcan",
        "id":   "610914868"
      }
    ]           // <--
}

You could try $obj = $obj->{'data'}[0] to get the first element in that array.

like image 67
kennytm Avatar answered Dec 15 '22 11:12

kennytm