json在php中剥离

时间:2016-08-29 09:50:29

标签: php json

您好我正在尝试从json文件中获取invoice_number对象。我在PHP中使用json_decode并获取未定义的属性错误。我不确定我做错了什么。

$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "hullsantarun-buyer@gmail.com", "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": "facilitator@gmail.com" }, "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'};

2 个答案:

答案 0 :(得分:1)

invoice_numbertransactions数组中对象的属性。

这将有效:

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

你总是可以这样做:

print_r($obj);

了解对象的实际外观。

答案 1 :(得分:0)

试试这个它适合你。 我已经尝试了你给出的json响应,我在你的代码中做了一些改变,它的工作尝试..

$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "hullsantarun-buyer@gmail.com", "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": "facilitator@gmail.com" }, "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;