dongyunwei8596 2017-06-21 11:27 采纳率: 100%
浏览 45
已采纳

PHP - 将JSON格式转换为数组

I am getting a response from a payment device in json format. I want to access that in array. Here is my json response

    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"}

I tried json decode but it is not working. it gives me empty data. I tried:

$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;

This gives me empty data.

  • 写回答

3条回答 默认 最新

  • dousha1831 2017-06-21 11:31
    关注

    As @Ian noted, your data is invalid. But if you strip off Without OrderId:- doInBackground: it will work.

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 arcgis中更新地理配准后几个图层分离怎么解决?
  • ¥15 想请教大家关于机器学习的问题
  • ¥15 虚幻UE保存Actor位置失败
  • ¥15 多个.nii合并为.nii.gz格式
  • ¥50 现在需要统计2023年全年 “签收时间(qssj)” 到 “提交时间(tjsj)” 小于半小时的业务量,但是要排除掉周末的时间,同时还要排除中午休息的时间。
  • ¥50 opencv模板匹配为何图像差别大仍然匹配数值高?
  • ¥15 如何成功完成主菜单和分菜单的代码编程C++
  • ¥15 怎样采集或者其它途径拿到全国最新个体工商户数据
  • ¥20 我是一名大学生,想学习java是自学还是报培训班呢
  • ¥15 pycharm该如何爬取网易云歌曲下的评论?