我是 PHP 和 JSON 的新手,所以希望有人能帮助我。
我有一个 PHP,第 3 方对其执行 POST 以提供 JSON 数据。发送的数据示例如下:
> { "created": 1326853478, "livemode": false, "id":
> "evt_00000000000000", "type": "charge.succeeded", "object":
> "event", "data": {
> "object": {
> "id": "ch_00000000000000",
> "object": "charge",
> "created": 1366838716,
> "livemode": false,
> "paid": true,
> "amount": 1000,
> "currency": "gbp",
> "refunded": false,
> "fee": 59,
> "fee_details": [
> {
> "amount": 59,
> "currency": "usd",
> "type": "stripe_fee",
> "description": "Stripe processing fees",
> "application": null,
> "amount_refunded": 0
> }
> ],
> "card": {
> "object": "card",
> "last4": "4242",
> "type": "Visa",
> "exp_month": 2,
> "exp_year": 2016,
> "fingerprint": "cniJDyeew54ashW6Iyr",
> "country": "US",
> "name": "wibble3",
> "address_line1": null,
> "address_line2": null,
> "address_city": null,
> "address_state": null,
> "address_zip": null,
> "address_country": null,
> "cvc_check": "pass",
> "address_line1_check": null,
> "address_zip_check": null
> },
> "captured": true,
> "failure_message": null,
> "amount_refunded": 0,
> "customer": null,
> "invoice": null,
> "description": null,
> "dispute": null
> } } }
我希望能够提取某些项目然后根据这些值进行处理。
使用这段代码,我可以很容易地提取“类型”:
$body = @file_get_contents('php://input');
$event_json = json_decode($body);
print $event_json->{'type'};
但是我无法提取“费用”,例如,这似乎是我成功从中提取价值的一个级别,或者“描述”又是另一个级别。
我希望能够从这个 JSON 字符串中只提取某些项目(例如,类型、费用、描述、Last4),但我完全迷失了。我很欣赏这可能是一项相当基本的任务,但我无处可去。
任何帮助,感激不尽!