2015-09-07 60 views
1

我得到以下JSON格式的響應,我希望它將其轉換爲PHP變量。如何獲取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 

請諮詢我如何做到這一點。

+0

最簡單的方式發佈,標記爲答案,如果它的東西你試圖找到。因此,每個參與尋找相同問題答案的人都會看到最適合你的東西。一路順風:) – aimme

回答

0

如果你想訪問你的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); 
0

只需簡單地使用json_decode();

$result= json_decode($jSon); 

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

JSON來陣列(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 

更多extract

使用$CreateTransactionResult['TransportKey']從JSON獲取傳輸密鑰。用於驗證密鑰的類似$CreateTransactionResult['ValidationKey']

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

     } 
    } 
0

在這種情況下,如果它的一個單一的和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 
*/