2016-11-08 39 views
1

我需要將JSON結果轉換爲PHP中的數組。 這裏我使用支付網關沙盒authorize.net。 我可以在JSON字符串中得到迴應($result)。但我不會轉換爲php陣列使用json_decode如何使用PHP將JSON字符串數據轉換爲數組?

$ output = json_decode($ result,true);的print_r($輸出);

示例代碼

<?php 
    $data=array("createTransactionRequest" => array(
        "merchantAuthentication" => array(
         "name" => "2bU77DwM", 
         "transactionKey" => "92x86d7M7f6NHK98" 
        ), 
        "refId" => "9898989898", 
        "transactionRequest" => array(
         "transactionType" => "authCaptureTransaction", 
         "amount" => "25", 
         "payment" => array(
          "creditCard" => array(
           "cardNumber" => "5424000000000015", 
           "expirationDate" => "1220", 
           "cardCode" => "999" 
          ) 
         ) 
        ) 
       ) 
      ); 
    $data_string = json_encode($data); 

    $ch = curl_init('https://apitest.authorize.net/xml/v1/request.api'); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     'Content-Type: application/json', 
     'Content-Length: ' . strlen($data_string)) 
    ); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $result = curl_exec($ch); 
    curl_close($ch);   
    print_r($result); //Print the JSON data 

    //Try to convert JSON into array 
    $output = json_decode($result,true); 
    print_r($output); //Print empty 

JSON響應print_r($result);

http://www.jsoneditoronline.org/?id=c75c5a6a4c247ad2f0aaf7c801daad39

+0

你可以分享樣品json結果嗎? –

+0

那麼,如果你不給我們回覆的內容,那麼json格式化字符串的內容應該如何幫助? – arkascha

+0

如果'json_decode()'不能解碼字符串,那麼它是無效的JSON。 – arkascha

回答

1

嘗試下面的代碼,你會得到結果。

<?php 
    $data=array("createTransactionRequest" => array(
        "merchantAuthentication" => array(
         "name" => "2bU77DwM", 
         "transactionKey" => "92x86d7M7f6NHK98" 
        ), 
        "refId" => "9898989898", 
        "transactionRequest" => array(
         "transactionType" => "authCaptureTransaction", 
         "amount" => "25", 
         "payment" => array(
          "creditCard" => array(
           "cardNumber" => "5424000000000015", 
           "expirationDate" => "1220", 
           "cardCode" => "999" 
          ) 
         ) 
        ) 
       ) 
      ); 
    $data_string = json_encode($data); 

    $ch = curl_init('https://apitest.authorize.net/xml/v1/request.api'); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     'Content-Type: application/json', 
     'Content-Length: ' . strlen($data_string)) 
    ); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $result = curl_exec($ch); 
    curl_close($ch);   

// below is my code 
$final_result = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result), true); 
echo "<pre>"; 
print_r($final_result); 

?> 

你只需要使用
$輸出= json_decode( 的preg_replace( '/ \ x00- \ x1F的\ x80- \ XFF] /', '',$結果),真) ;

print_r($ output);

我查過了!它爲我工作。 希望這會有所幫助!

+0

謝謝!它工作正常。 –