如何访问php中嵌套在另一个对象中的对象的成员?
我有一个JSON对象,它是从向paypal sandbox API发出的请求返回的。但是,我在访问此对象中的某些成员时遇到困难。例如,我想访问位于事务对象中的相关资源对象的total属性如何访问php中嵌套在另一个对象中的对象的成员?,php,json,Php,Json,我有一个JSON对象,它是从向paypal sandbox API发出的请求返回的。但是,我在访问此对象中的某些成员时遇到困难。例如,我想访问位于事务对象中的相关资源对象的total属性 $result = $payment->execute($execute, $apiContext); $data = json_decode($result); //$data = $result->toJSON(); 我得到以下错误 注意:试图在第行的路径\u到\u文件中获取非对象的属性 67
$result = $payment->execute($execute, $apiContext);
$data = json_decode($result);
//$data = $result->toJSON();
我得到以下错误
注意:试图在第行的路径\u到\u文件中获取非对象的属性
67
当我尝试访问付款人对象时
print_r($data->transactions);
每当我试图访问嵌套在事务对象中的属性时,我也会收到相同的错误消息。例如:
echo $data->transaction[0]->amount[0]->total;
我按照以下链接中找到的步骤寻找解决方案,但我的努力是徒劳的
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 ) ) ) ) ) ) )
您的JSON无效。json_decode()将返回null。这就是为什么您会收到错误消息
details":{
"subtotal":"0.99"
}
缺少双引号
修复json后,我可以访问$json->事务,没有问题
如果您的json被解码为数组,请使用
$json['transactions'];
如果您的json是一个数组,您可以访问第一个总数,例如
$json['transactions'][0]['amount']['total'];
还是有对象
$json_dec = json_decode($json);
$json_dec->transactions[0]->amount->total;
在您使用$data的情况下,您似乎已经进入了事务数组
所以
首先,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->transaction,您首先需要使用函数json\u decode()解码此json,然后您将能够通过这种方式访问$data->transaction响应看起来像是一个关联,而不是一个关联。尝试:
print\r($data[transactions])代码>
您会遇到如下错误:注意:当您不尝试访问对象时,尝试获取非对象的属性。我们访问如下数组:someArray['transactions']
和对象:someObject->transactions
如果您使用的是来自laravel的toJson方法,我认为它返回的是字符串而不是对象?使用json_decode with assoc=true,然后像常规关联数组一样访问成员@Gabriel B.R我尝试了您的解决方案,它会生成相同的错误消息。您发布的print_R($data)
中没有事务
键,在发布的代码中,您似乎应该得到如下值:数据[0][yourKey]
JSON不返回null。当我格式化JSON对象时,这一定是一个错误。我进行了更正,但仍然收到错误消息。在JSON解码后打印($JSON),查看其数组或对象是否对解码后的JSON执行var_dump(),以查看它是否正在解码打印($data),并以PHP数组的形式输出数据,看起来像数组([id]=>PAY-2LE977621P903670ALLJPP2I[intent]=>sale[state]=>approved[cart]。注意,json_decode($result,true)true语句已添加到json_decode函数中。我尝试了此操作,但最终结果仍然没有更改。@andreid在进行var_dump(json_decode($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"
}
]
}]
}]
}