2016-08-29 14 views
-2

こんにちは、私はjsonファイルからinvoice_numberオブジェクトを取得しようとしています。私はPHPでjson_decodeを使用して、未定義のプロパティエラーを取得しています。私が間違っていることがわかりません。json stripping in php

$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "[email protected]", "first_name": "test", "last_name": "buyer", "payer_id": "xxxxx", "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" }, "country_code": "GB", "billing_address": { "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } } }, "transactions": [ { "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00", "shipping": "0.00" } }, "payee": { "merchant_id": "xxx", "email": "[email protected]" }, "description": "Pay for all your santa fun", "invoice_number": "57c4007c11a60", "item_list": { "items": [ { "name": "wayne : \u00a310 ; ", "price": "10.00", "currency": "GBP", "quantity": 1 } ], "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } }, "related_resources": [ { "sale": { "id": "23V89902U7365974Y", "state": "completed", "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00" } }, "payment_mode": "INSTANT_TRANSFER", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "transaction_fee": { "value": "0.54", "currency": "GBP" }, "parent_payment": "PAY-2C432486DK345933DK7CAA7I", "create_time": "2016-08-29T09:29:53Z", "update_time": "2016-08-29T09:29:54Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "billing_plan_units": [ { "billing_plan_approved": false } ], "redirect_urls": { "return_url": "https://www.hullsantarun.org/php/pay.php?success=true&paymentId=PAY-2C432486DK345933DK7CAA7I", "cancel_url": "https://www.hullsantarun.org/php/pay.php?success=false" }, "create_time": "2016-08-29T09:29:54Z", "update_time": "2016-08-29T09:29:53Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "self", "method": "GET" } ] }'; 



$obj = json_decode($json); 

print $obj->{'invoice_number'}; 

答えて

0

これはあなたに役立ちます。

$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "[email protected]", "first_name": "test", "last_name": "buyer", "payer_id": "xxxxx", "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" }, "country_code": "GB", "billing_address": { "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } } }, "transactions": [ { "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00", "shipping": "0.00" } }, "payee": { "merchant_id": "xxx", "email": "[email protected]" }, "description": "Pay for all your santa fun", "invoice_number": "57c4007c11a60", "item_list": { "items": [ { "name": "wayne : \u00a310 ; ", "price": "10.00", "currency": "GBP", "quantity": 1 } ], "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } }, "related_resources": [ { "sale": { "id": "23V89902U7365974Y", "state": "completed", "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00" } }, "payment_mode": "INSTANT_TRANSFER", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "transaction_fee": { "value": "0.54", "currency": "GBP" }, "parent_payment": "PAY-2C432486DK345933DK7CAA7I", "create_time": "2016-08-29T09:29:53Z", "update_time": "2016-08-29T09:29:54Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "billing_plan_units": [ { "billing_plan_approved": false } ], "redirect_urls": { "return_url": "https://www.hullsantarun.org/php/pay.php?success=true&paymentId=PAY-2C432486DK345933DK7CAA7I", "cancel_url": "https://www.hullsantarun.org/php/pay.php?success=false" }, "create_time": "2016-08-29T09:29:54Z", "update_time": "2016-08-29T09:29:53Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "self", "method": "GET" } ] }'; 

$obj = json_decode($json); 
print $obj->transactions[0]->invoice_number; 

OR

echo $obj->transactions[0]->invoice_number; 
1

invoice_numbertransactions、配列内のオブジェクトのプロパティです..私はあなたの与えられたJSONレスポンスを試していると私はあなたのコード内のいくつかの変更を行うと、それは仕事をしてみてくださいです。

これは動作します:

$obj->transactions[0]->invoice_number; 

いつでも行うことができます。

print_r($obj); 

オブジェクトが実際にどのように見えるかを確認します。