简体   繁体   中英

How to get JSON response in to PHP variables

I am getting following response in JSON format and I want it to convert it into PHP variables.

JSON:

{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900"
,"ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}

output should be PHP:

$transkey = aa900d54-7bfb-47e9-a5de-e423ec34a900;
$vkey = fbb28b32-f439-4801-a434-99c70aa388ca

please advice me how to do it.

If you want to access your json try to decode it first:

$result = json_decode($yourJSON, true);

foreach($result['CreateTransactionResponse'] as $key => $val){
   echo $transkey = 'TransportKey= ' . $val['TransportKey'] . '<br/>;
   echo $vkey = 'ValidationKey= ' . $val['ValidationKey'];
}

Or if it is an array of JSON's

$result = json_decode($yourJSON, true);

$data = [];
foreach($result['CreateTransactionResponse'] as $key => $val){
   $data[] = [
        'TransportKey' => $val['TransportKey'],
        'ValidationKey' => $val['ValidationKey']
   ];
}
print_r($data);

Just simply use json_decode();

 $result= json_decode($jSon);

 var_dump($result); // to see the output

json to array( json_decode ) and then extract from array.

$arr = json_decode($json, true);
extract($arr);
var_dump($CreateTransactionResponse);

Output:

array (size=1)
  'CreateTransactionResult' => 
    array (size=3)
      'TransportKey' => string 'aa900d54-7bfb-47e9-a5de-e423ec34a900' (length=36)
      'ValidationKey' => string 'fbb28b32-f439-4801-a434-99c70aa388ca' (length=36)
      'Messages' => 
        array (size=0)
          empty

More about extract

use $CreateTransactionResult['TransportKey'] to access Transport Key from JSON. Similarly $CreateTransactionResult['ValidationKey'] for Validation Key.

    try this code it will work  
$JSON='{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900" ,"ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}';    

    $arr=json_decode($JSON, TRUE);
    foreach ($arr as  $value) {

    foreach ($arr['CreateTransactionResponse'] as $key => $var) {
        echo 'TransportKey = '.$var['TransportKey'].'<br>';
        echo 'ValidationKey = '.$var['ValidationKey'].'<br>';
        foreach ($var['Messages'] as $key => $msg) {


        echo 'Messages = '.$msg.'<br>';
    }

        }
    }

In this case,If its a single and TransportKey and a single ValidationKey value (not an array/object is passed) at a time, this is the simplest . Else if object contains objects or inside objects that we want to use or convert to variable, should use a foreach to loop through the object.

//Debuggig
//The string you provided is converted to a json object 
//In your case if it is a json object already pass directly it to $j
//below is just for debugging and understanding
//$json='{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900","ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}';
//$j=json_decode($json);

$transkey=$j->CreateTransactionResponse->CreateTransactionResult->TransportKey;
$vkey=$j->CreateTransactionResponse->CreateTransactionResult->ValidationKey;

echo $transkey."</br>";
echo $vkey."<br/>";
/*result as said: 
aa900d54-7bfb-47e9-a5de-e423ec34a900
fbb28b32-f439-4801-a434-99c70aa388ca
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM