【发布时间】:2016-11-08 10:20:33
【问题描述】:
我需要将 JSON 结果转换为 PHP 中的数组。
这里我使用authorize.net 支付网关沙箱。
我可以在JSON 字符串中得到响应($result)。但我不会使用json_decode 转换为 php 数组
$output = json_decode($result,true); print_r($output);
示例代码
<?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
【问题讨论】:
-
可以分享一下示例json结果吗?
-
好吧,如果你不给我们回复的内容,那我们该如何帮助呢?
-
如果
json_decode()无法解码字符串,那么它不是有效的JSON。 -
请运行示例代码并查看 JSON 响应。
-
你遇到了什么错误?