Separate string variables json

1

I have the following string:

string(292) "
{"access_token":"ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa",
"token_type":"Bearer",
"expires_in":300,
"refresh_token":"ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73",
"scope":"user_account send_currency currency_exchange"
}" 

How could I separate this string into variables and show the value of each? echo $access_token ? or echo $token_type ?

    
asked by anonymous 20.06.2017 / 15:24

2 answers

1

Hello. First of all you will use json_decode, after that you have an object with the desired properties. Once you have them in hand, you can access via object or externalize the variables as requested. Here's an example:

<?php

$json_string = '{
    "access_token": "ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa",
    "token_type": "Bearer",
    "expires_in": 300,
    "refresh_token": "ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73",
    "scope": "user_account send_currency currency_exchange"
}';

$json_parse = json_decode($json_string);

echo "<pre>";
echo "access_token  :" . $json_parse->access_token  . PHP_EOL;
echo "token_type    :" . $json_parse->token_type    . PHP_EOL;
echo "expires_in    :" . $json_parse->expires_in    . PHP_EOL;
echo "refresh_token :" . $json_parse->refresh_token . PHP_EOL;
echo "scope         :" . $json_parse->scope         . PHP_EOL;
echo "</pre>";

// Transforma o escopo das variáveis acessíveis externamente
// O item deve ser um array, como o json_decode transforma em objeto
// precisamos transformar em array
extract((array) $json_parse);

echo "<pre>";
echo "access_token  :" . $access_token  . PHP_EOL;
echo "token_type    :" . $token_type    . PHP_EOL;
echo "expires_in    :" . $expires_in    . PHP_EOL;
echo "refresh_token :" . $refresh_token . PHP_EOL;
echo "scope         :" . $scope         . PHP_EOL;
echo "</pre>";

?>

Result:

access_token  :ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa
token_type    :Bearer
expires_in    :300
refresh_token :ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73
scope         :user_account send_currency currency_exchange

access_token  :ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa
token_type    :Bearer
expires_in    :300
refresh_token :ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73
scope         :user_account send_currency currency_exchange

You can view the execution of this code this link .

    
20.06.2017 / 16:02
0

If you have a string you need to convert to JSON and then access the property you want.

To:

  • convert to json: use json_decode
  • access a property: $json -> token_type

Example:

$string = '{
    "access_token":"ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa",
    "token_type":"Bearer",
    "expires_in":300,
    "refresh_token":"ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73",
    "scope":"user_account send_currency currency_exchange"
}';
$json = json_decode($string);
echo $json -> token_type;

Example online: link

    
20.06.2017 / 15:58