PHP - 将JSON格式转换为数组

时间:2017-06-21 11:27:04

标签: php json

我收到了json格式的付款设备的回复。我想在数组中访问它。 这是我的json回复

    Without OrderId:- 
doInBackground: {"amount":"1.00","reason":"Transaction declined by card","transactionId":"219775","transactionData":"{\"result\":\"success\",\"transactionId\":219775,\"billNumber\":\"101:879209:675466.0\",\"applicationId\":\"A0000001523010\",\"tsi\":\"E800\",\"tvr\":\"0800048000\",\"date\":\"21-06-2017\",\"currency\":\"NA\",\"tcResult\":\"0\",\"amount\":\"1.00\",\"terminalId\":\"98207200\",\"tipAmount\":\"0.00\",\"time\":\"15:41:58\",\"batchNumber\":\"1\",\"receiptLink\":\"http:\\\/\\\/test.mosambee.in\\\/ROOT1\\\/ViewReciept.do?action=mobileReceipt&txnId=219775\",\"merchantId\":\"P587956\",\"transactionStatus\":\"Declined\",\"deviceId\":1449,\"cardHolderName\":\"RK9018388863             \\\/\",\"invoiceNumber\":\"40\",\"approvalCode\":\"689653\",\"transactionMode\":\"CHIP\",\"cardType\":\"Invalid Card\",\"message\":\"Transaction declined by card\",\"transactionType\":\"Sale\",\"retrievalReferenceNumber\":\"000000219775\",\"cashBack\":\"NA\",\"cardNumber\":\"652163XXXXXX1698\",\"isSignatureRequired\":false,\"orderId\":\"101\",\"shipperId\":\"879209\"}","result":"false","reasonCode":"NA"}

我尝试了json解码,但它无法正常工作。它给了我空数据。     我试过了:

$data = json_decode('Without OrderId:- 
            doInBackground: {"amount":"1.00","reason":"Transaction declined by card","transactionId":"219775","transactionData":"{\"result\":\"success\",\"transactionId\":219775,\"billNumber\":\"101:879209:675466.0\",\"applicationId\":\"A0000001523010\",\"tsi\":\"E800\",\"tvr\":\"0800048000\",\"date\":\"21-06-2017\",\"currency\":\"NA\",\"tcResult\":\"0\",\"amount\":\"1.00\",\"terminalId\":\"98207200\",\"tipAmount\":\"0.00\",\"time\":\"15:41:58\",\"batchNumber\":\"1\",\"receiptLink\":\"http:\\\/\\\/test.mosambee.in\\\/ROOT1\\\/ViewReciept.do?action=mobileReceipt&txnId=219775\",\"merchantId\":\"P587956\",\"transactionStatus\":\"Declined\",\"deviceId\":1449,\"cardHolderName\":\"RK9018388863             \\\/\",\"invoiceNumber\":\"40\",\"approvalCode\":\"689653\",\"transactionMode\":\"CHIP\",\"cardType\":\"Invalid Card\",\"message\":\"Transaction declined by card\",\"transactionType\":\"Sale\",\"retrievalReferenceNumber\":\"000000219775\",\"cashBack\":\"NA\",\"cardNumber\":\"652163XXXXXX1698\",\"isSignatureRequired\":false,\"orderId\":\"101\",\"shipperId\":\"879209\"}","result":"false","reasonCode":"NA"}', true);
        return $data;

这给了我空数据。

3 个答案:

答案 0 :(得分:1)

正如@Ian所说,您的数据无效。但是,如果你剥离Without OrderId:- doInBackground:它会起作用。

$data = json_decode('{"amount":"1.00","reason":"Transaction declined by card","transactionId":"219775","transactionData":"{\"result\":\"success\",\"transactionId\":219775,\"billNumber\":\"101:879209:675466.0\",\"applicationId\":\"A0000001523010\",\"tsi\":\"E800\",\"tvr\":\"0800048000\",\"date\":\"21-06-2017\",\"currency\":\"NA\",\"tcResult\":\"0\",\"amount\":\"1.00\",\"terminalId\":\"98207200\",\"tipAmount\":\"0.00\",\"time\":\"15:41:58\",\"batchNumber\":\"1\",\"receiptLink\":\"http:\\\/\\\/test.mosambee.in\\\/ROOT1\\\/ViewReciept.do?action=mobileReceipt&txnId=219775\",\"merchantId\":\"P587956\",\"transactionStatus\":\"Declined\",\"deviceId\":1449,\"cardHolderName\":\"RK9018388863             \\\/\",\"invoiceNumber\":\"40\",\"approvalCode\":\"689653\",\"transactionMode\":\"CHIP\",\"cardType\":\"Invalid Card\",\"message\":\"Transaction declined by card\",\"transactionType\":\"Sale\",\"retrievalReferenceNumber\":\"000000219775\",\"cashBack\":\"NA\",\"cardNumber\":\"652163XXXXXX1698\",\"isSignatureRequired\":false,\"orderId\":\"101\",\"shipperId\":\"879209\"}","result":"false","reasonCode":"NA"}');
print_r($data);

答案 1 :(得分:0)

您没有有效的回复,您期望什么?

Without OrderId:- doInBackground:

无效。在此字符串上运行json_decode后,如果运行json_last_error_msg,则会出现“语法错误。”

在将消息传递给json_decode之前,您必须删除此消息中不是键值对的部分。

您也可以通过在https://jsonlint.com/上测试字符串来验证这一点。

答案 2 :(得分:-1)

做: - > echo json_decode('json / array name');