简体   繁体   中英

Decoding the JSON in PHP

I need help decoding the JSON below in PHP. I am facing a challenge doing that so I need assistance. Below is my JSON and I need the values of the attributes in the return parameter as well as the RESULT array.

{"return":"{"MESSAGE":"INVOICE IS ACCEPPTED - ECHO THE PAYMENT URL","CODE":"0","SUCCESS":true,"RESULT":[{"DESCRIPTION":"Payment for business registration","PAYMENTTOKEN":"6354c166-cdb1-459a-8a7c-e23f9d3a0dd6","APIPAYREDIRECTURL":"https://testcalpay.caleservice.net/pay/secure/index.php?paytoken=6354c166-cdb1-459a-8a7c-e23f9d3a0dd6","QRLCODEURL":"https://testcalpay.caleservice.net/paymentqrs/BK3NUFI936.png","PAYMENTCODE":"BK3NUFI936","MOMOSTATUS":"1","ORDERCODE":"INV2022003","MOMOPAYMENT":"FAILED","SHORTPAYCODE":"10000645"}]}"}

And this is my code:

$msg = $obj['return']->MESSAGE; var_dump($msg);

I get the error:
Attempt to read property "MESSAGE" on string in

The json you posted is invalid. Check out the screenshot below. The object seems to be enclosed by double quotations. I guess it is supposed to be a regular object so don't enclose it by double quotations unless you want it as a string data.

在此处输入图像描述

And below is the json in which double quotations are removed from the object.

{
    "return": {
        "MESSAGE": "INVOICE IS ACCEPPTED - ECHO THE PAYMENT URL",
        "CODE": "0",
        "SUCCESS": true,
        "RESULT": [
            {
                "DESCRIPTION": "Payment for business registration",
                "PAYMENTTOKEN": "6354c166-cdb1-459a-8a7c-e23f9d3a0dd6",
                "APIPAYREDIRECTURL": "https://testcalpay.caleservice.net/pay/secure/index.php?paytoken=6354c166-cdb1-459a-8a7c-e23f9d3a0dd6",
                "QRLCODEURL": "https://testcalpay.caleservice.net/paymentqrs/BK3NUFI936.png",
                "PAYMENTCODE": "BK3NUFI936",
                "MOMOSTATUS": "1",
                "ORDERCODE": "INV2022003",
                "MOMOPAYMENT": "FAILED",
                "SHORTPAYCODE": "10000645"
            }
        ]
    }
}

Answer to your question, you could decode json like above using json_decode() . See example below.

Working example

<?php
// Enter your code here, enjoy!
$obj = '{"return":{"MESSAGE":"INVOICE IS ACCEPPTED - ECHO THE PAYMENT URL","CODE":"0","SUCCESS":true,"RESULT":[{"DESCRIPTION":"Payment for business registration","PAYMENTTOKEN":"6354c166-cdb1-459a-8a7c-e23f9d3a0dd6","APIPAYREDIRECTURL":"https://testcalpay.caleservice.net/pay/secure/index.php?paytoken=6354c166-cdb1-459a-8a7c-e23f9d3a0dd6","QRLCODEURL":"https://testcalpay.caleservice.net/paymentqrs/BK3NUFI936.png","PAYMENTCODE":"BK3NUFI936","MOMOSTATUS":"1","ORDERCODE":"INV2022003","MOMOPAYMENT":"FAILED","SHORTPAYCODE":"10000645"}]}}';

$parsed_json = json_decode($obj);
var_dump($parsed_json->return);
var_dump($parsed_json->return->RESULT);

# 👇 The output

// object(stdClass)#1 (4) {
//   ["MESSAGE"]=>
//   string(43) "INVOICE IS ACCEPPTED - ECHO THE PAYMENT URL"
//   ["CODE"]=>
//   string(1) "0"
//   ["SUCCESS"]=>
//   bool(true)
//   ["RESULT"]=>
//   array(1) {
//     [0]=>
//     object(stdClass)#2 (9) {
//       ["DESCRIPTION"]=>
//       string(33) "Payment for business registration"
//       ["PAYMENTTOKEN"]=>
//       string(36) "6354c166-cdb1-459a-8a7c-e23f9d3a0dd6"
//       ["APIPAYREDIRECTURL"]=>
//       string(101) "https://testcalpay.caleservice.net/pay/secure/index.php?paytoken=6354c166-cdb1-459a-8a7c-e23f9d3a0dd6"
//       ["QRLCODEURL"]=>
//       string(60) "https://testcalpay.caleservice.net/paymentqrs/BK3NUFI936.png"
//       ["PAYMENTCODE"]=>
//       string(10) "BK3NUFI936"
//       ["MOMOSTATUS"]=>
//       string(1) "1"
//       ["ORDERCODE"]=>
//       string(10) "INV2022003"
//       ["MOMOPAYMENT"]=>
//       string(6) "FAILED"
//       ["SHORTPAYCODE"]=>
//       string(8) "10000645"
//     }
//   }
// }
// array(1) {
//   [0]=>
//   object(stdClass)#2 (9) {
//     ["DESCRIPTION"]=>
//     string(33) "Payment for business registration"
//     ["PAYMENTTOKEN"]=>
//     string(36) "6354c166-cdb1-459a-8a7c-e23f9d3a0dd6"
//     ["APIPAYREDIRECTURL"]=>
//     string(101) "https://testcalpay.caleservice.net/pay/secure/index.php?paytoken=6354c166-cdb1-459a-8a7c-e23f9d3a0dd6"
//     ["QRLCODEURL"]=>
//     string(60) "https://testcalpay.caleservice.net/paymentqrs/BK3NUFI936.png"
//     ["PAYMENTCODE"]=>
//     string(10) "BK3NUFI936"
//     ["MOMOSTATUS"]=>
//     string(1) "1"
//     ["ORDERCODE"]=>
//     string(10) "INV2022003"
//     ["MOMOPAYMENT"]=>
//     string(6) "FAILED"
//     ["SHORTPAYCODE"]=>
//     string(8) "10000645"
//   }
// }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM