如何访问嵌套在php中另一个对象内的对象的成员?

时间:2018-04-15 06:04:43

标签: php json

我有一个JSON对象,该对象是从对paypal沙盒API的请求返回的。但是,我在访问此对象的某些成员时遇到了困难。例如,我想访问位于事务对象中的related_resources对象的total属性。

$result = $payment->execute($execute, $apiContext);
$data = json_decode($result);
//$data = $result->toJSON();

我收到以下错误

  

注意:尝试在路径上获取path_to_file中的非对象属性   67

当我尝试访问付款人对象时。

print_r($data->transactions);

每当我尝试访问嵌套在事务对象中的属性时,我也会收到相同的错误消息。例如:

echo $data->transaction[0]->amount[0]->total;

我按照以下链接How do I extract data from JSON with PHP?中找到的步骤找到了解决方案,但我的努力是徒劳的。

JSON数据的格式为:

{
    "id":"PAY-5S764194SH917514SLLJNSZA","intent":"sale","state":"approved","cart":"5LM08388X5236923U",
    "transactions":
    [
        {
            "amount":
            {
                "total":"0.99","currency":"USD",
                "details":{}
            },
            "payee":
            {
                "merchant_id":"MJBN5EPGYYLZE","email":"Test-Business-facilitator@gmail.com"
            },
            "item_list":
            {
                "shipping_address":
                {
                    "recipient_name":"Test Personal","line1":"1 Main St","city":"San Jose","state":"CA","postal_code":"95131","country_code":"US"
                }
            },
            "related_resources":
            [
                {
                    "sale":
                    {
                        "id":"9N957492L93533703","state":"completed",
                        "amount":{
                            "total":"0.99","currency":"USD", 
                            "details":
                            {
                                "subtotal":"0.99"
                            }
                        }
                    },"transaction_fee":{"value":"0.33","currency":"USD"},
                    "links":
                    [
                        {
                            "href":"https://","rel":"self","method":"GET"
                        },
                        {
                            "href":"","rel":"refund","method":"POST"
                        },
                        {
                            "href":"https:/","method":"GET"
                        }
                    ]
                }
            ]
        }
    ]
}

这是执行以下操作的输出:

$data = json_decode($result, true);
print_r($data);

Array ( [0] => Array ( [amount] => Array ( [total] => 0.99 [currency] => USD [details] => Array ( ) ) [payee] => Array ( [merchant_id] => MJBN5EPGYYLZE [email] => Test-Business-facilitator@discoverytechnologiesja.com ) [item_list] => Array ( [shipping_address] => Array ( [recipient_name] => Test Personal [line1] => 1 Main St [city] => San Jose [state] => CA [postal_code] => 95131 [country_code] => US ) ) [related_resources] => Array ( [0] => Array ( [sale] => Array ( [id] => 0BK223494W308401A [state] => completed [amount] => Array ( [total] => 0.99 [currency] => USD [details] => Array ( [subtotal] => 0.99 ) ) [payment_mode] => INSTANT_TRANSFER [protection_eligibility] => ELIGIBLE [protection_eligibility_type] => ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE [transaction_fee] => Array ( [value] => 0.33 [currency] => USD ) [parent_payment] => PAY-8W230634SJ018825BLLJPUFA [create_time] => 2018-04-15T07:07:37Z [update_time] => 2018-04-15T07:07:37Z [links] => Array ( [0] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/sale/0BK223494W308401A [rel] => self [method] => GET ) [1] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/sale/0BK223494W308401A/refund [rel] => refund [method] => POST ) [2] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-8W230634SJ018825BLLJPUFA [rel] => parent_payment [method] => GET ) ) ) ) ) ) ) 

3 个答案:

答案 0 :(得分:0)

您的JSON无效。 json_decode()将返回null。这就是你收到错误信息的原因。

details":{
    "subtotal":"0.99"
}

缺少双引号

修好你的json之后,我可以访问$ json->交易没问题

如果您的json被解码为数组,请使用

$json['transactions'];

如果您的json是一个数组,您可以访问第一个总数,例如

$json['transactions'][0]['amount']['total'];

OR与对象

$json_dec = json_decode($json);
$json_dec->transactions[0]->amount->total;

在使用$ data的情况下,您似乎已经进入了Transactions数组。

所以

$data[0]['amount']['total'];

答案 1 :(得分:0)

响应看起来像是关联Array而不是Object。尝试:print_r($data[transactions]);

当您不尝试访问对象时,会出现类似Notice: Trying to get property of non-object的错误。我们访问的数组如:someArray['transactions']和对象类似:someObject->transactions

答案 2 :(得分:-1)

首先你在json中有错误

由json更正:

{
    "id": "PAY-5S764194SH917514SLLJNSZA",
    "intent": "sale",
    "state": "approved",
    "cart": "5LM08388X5236923U",
    "transactions": [{
        "amount": {
            "total": "0.99",
            "currency": "USD",
            "details": {}
        },
        "payee": {
            "merchant_id": "MJBN5EPGYYLZE",
            "email": "Test-Business-facilitator@gmail.com"
        },
        "item_list": {
            "shipping_address": {
                "recipient_name": "Test Personal",
                "line1": "1 Main St",
                "city": "San Jose",
                "state": "CA",
                "postal_code": "95131",
                "country_code": "US"
            }
        },
        "related_resources": [{
            "sale": {
                "id": "9N957492L93533703",
                "state": "completed",
                "amount": {
                    "total": "0.99",
                    "currency": "USD",
                    "details": {
                        "subtotal": "0.99"
                    }
                }
            },
            "transaction_fee": {
                "value": "0.33",
                "currency": "USD"
            },
            "links": [{
                    "href": "https://",
                    "rel": "self",
                    "method": "GET"
                },
                {
                    "href": "",
                    "rel": "refund",
                    "method": "POST"
                },
                {
                    "href": "https:/",
                    "method": "GET"
                }
            ]
        }]
    }]
}

其次,为了您想要 $ data->事务,您首先需要使用函数 json_decode()解码此json然后您将能够以这种方式访问​​ $ data->事务