How to decode the following JSON? PHP

Asked

Viewed 615 times

2

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

I want to get the USER ID with json_decode.

As caught?

2 answers

5


$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

0

just use the variable containing 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'};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.