簡體   English   中英

如何在PHP變量中獲取JSON響應

[英]How to get JSON response in to PHP variables

我正在獲取JSON格式的響應,並且希望它將其轉換為PHP變量。

JSON:

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

輸出應為PHP:

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

請告訴我該怎么做。

如果要訪問您的json,請嘗試先對其進行解碼:

$result = json_decode($yourJSON, true);

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

或者,如果它是JSON的數組

$result = json_decode($yourJSON, true);

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

只需簡單地使用json_decode();

 $result= json_decode($jSon);

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

json到array( json_decode ),然后從數組中extract

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

輸出:

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

有關提取的更多信息

使用$CreateTransactionResult['TransportKey']從JSON訪問傳輸密鑰。 類似地, $CreateTransactionResult['ValidationKey']用於驗證密鑰。

    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>';
    }

        }
    }

在這種情況下,如果一次同時使用一個和TransportKey以及一個ValidationKey值(不傳遞數組/對象),則這是最簡單的 否則,如果對象包含我們要使用或轉換為變量的對象或內部對象,則應使用foreach該對象。

//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
*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM