How to decode the following JSON? PHP

2
{
   "data": {
      "app_id": "0000000",
      "scopes": [
         "public_profile"
      ],
      "user_id": "00000000"
   }
}

I want to get the USER ID with json_decode.

How do I get it?

    
asked by anonymous 06.07.2016 / 19:07

2 answers

3
$json = '{
   "data": {
      "app_id": "0000000",
      "scopes": [
         "public_profile"
      ],
      "user_id": "00000000"
   }
}';

//json_decode retornando um objeto
$object = json_decode($json);
$user_id = $object->{data}->{user_id};

//json_decode retornando um array associativo
$array_associativo = json_decode($json,true);
$user_id = $array_associativo['data']['user_id'];

Reference: php.net

    
06.07.2016 / 20:54
0

Just use the variable that contains the JSON and use the index:

$json = json_decode(file_get_contents('php://input',true)); // <---- Usei esse exemplo, mas você pode usar qualquer "raw JSON"

// $json no momento contém o objeto total

$data = $json->{'data'};
$user_id = $data->{'user_id'};
    
06.07.2016 / 19:23