Separate string variables json

Asked

Viewed 463 times

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 ?

  • Ever tried to use json_encode($texto);?

  • Yeah, so far it’s not working

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. Follow 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>";

?>

Upshot:

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 at this link.

  • great, it was clear But it is being generated like this link: http://www.agbsites.com/teste.php with string(323), as it would look like ?

  • I believe you are using var_dump to output this text, just echo it. If possible, put the code that is generating this output to make it easier to help you.

  • www.agbsites.com/gerar.txt .

  • I got Bruno, I only changed var_dump to echo on the way out and integrated it with the output variable $Response, $Response = curl_exec($ch); curl_close($ch); $json_parse = json_decode($Response); Thanks!

0

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

To:

  • convert to json: uses the 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;

Online example: https://ideone.com/i4eil5

  • Hello Sergio, thank you But in the case it is being generated as it is in the link below: http://www.agbsites.com/teste.php with the string(323) as it would look like ?

  • @Wellingtonalves which PHP generates this string?

  • On this link, Sergio, is an API www.agbsites.com/gerar.txt Hence I need to separate the generated variables

  • Thanks Sergio, I managed to solve

  • @Wellingtonalves was out. Yeah, in view of var_dump($response); you should only have echo $response; or directly $json = json_decode($response); and then as I indicated in the reply.

Browser other questions tagged

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