如何使用PHP将JSON字符串数据转换为数组?

时间:2016-11-08 10:20:33

标签: php arrays json curl

我需要在PHP中将JSON结果转换为数组。 我在这里使用authorize.net付款网关沙箱。 我可以在$result字符串中获得响应(JSON)。但我不会使用json_decode

转换为php数组
  

$ 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
来自print_r($result);

JSON回复

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

1 个答案:

答案 0 :(得分:2)

尝试以下代码即可获得结果。

<?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);

?>
  

你只需要使用
  $ output = json_decode(   preg_replace(&#39; / [\ x00- \ x1F \ x80- \ xFF] /&#39;,&#39;&#39;,$ result),true);

     

的print_r($输出);

我检查了一下!它为我工作。 希望这会有所帮助!