PHP:在JSON创建的对象中获取嵌套属性?

时间:2015-12-02 15:26:13

标签: php json

我正在尝试在通过JSON调用创建的对象中获取嵌套属性。

我不完全确定我所做的事情是否正确,因为我根本没有得到嵌套属性。

我只需要在文本文件中写下该值。

这是我的全部代码:

$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Do something with $event_json


//print_r($event_json);

$user = $event_json->data->object->lines->data->plan->id;

http_response_code(200); // PHP 5.4 or greater

//if ($event_json->type == "charge.failed") {
if ($event_json->type == 'invoice.payment_succeeded') {
$file = fopen("file.txt","w");
fwrite($file,$user);
fclose($file);
}

这就是我试图获取嵌套属性的方法:

$user = $event_json->data->object->lines->data->plan->id;

这是整个JSON回复:

{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "invoice.payment_succeeded",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2015-10-16",
  "data": {
    "object": {
      "id": "in_00000000000000",
      "object": "invoice",
      "amount_due": 1900,
      "application_fee": null,
      "attempt_count": 1,
      "attempted": true,
      "charge": "_00000000000000",
      "closed": true,
      "currency": "gbp",
      "customer": "cus_00000000000000",
      "date": 1449067309,
      "description": null,
      "discount": null,
      "ending_balance": 0,
      "forgiven": false,
      "lines": {
        "data": [
          {
            "id": "sub_7SXJcYv81SUnhB",
            "object": "line_item",
            "amount": 1900,
            "currency": "gbp",
            "description": null,
            "discountable": true,
            "livemode": true,
            "metadata": {},
            "period": {
              "start": 1451747394,
              "end": 1454425794
            },
            "plan": {
              "id": "someemail@email.com",
              "object": "plan",
              "amount": 1900,
              "created": 1449067298,
              "currency": "gbp",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {},
              "name": "someemail@email.com",
              "statement_descriptor": null,
              "trial_period_days": null
            },
            "proration": false,
            "quantity": 1,
            "subscription": null,
            "type": "subscription"
          }
        ],
        "total_count": 1,
        "object": "list",
        "url": "/v1/invoices/in_17DbnlFMZc35g8A2r4s2DYmb/lines"
      },
      "livemode": false,
      "metadata": {},
      "next_payment_attempt": null,
      "paid": true,
      "period_end": 1449067309,
      "period_start": 1449067309,
      "receipt_number": null,
      "starting_balance": 0,
      "statement_descriptor": null,
      "subscription": "sub_00000000000000",
      "subtotal": 1900,
      "tax": null,
      "tax_percent": null,
      "total": 1900,
      "webhooks_delivered_at": 1449067325
    }
  }
}

我需要得到的是这个的价值:"id": "someemail@email.com",

有人可以就此问题提出建议吗?

对标记为重复和投票的人的提问:

您提供给其他问题的答案与我在代码中所做的相同!所以这不应该被标记为重复,因为我要求帮助找出我做错了什么或错过了什么!

很好地解释这个问题的哪一部分与您指出的问题重复?!?

感谢您的贡献。

这是print_r给我的:

stdClass Object
(
    [created] => 1326853478
    [livemode] => 
    [id] => evt_00000000000000
    [type] => invoice.payment_succeeded
    [object] => event
    [request] => 
    [pending_webhooks] => 1
    [api_version] => 2015-10-16
    [data] => stdClass Object
        (
            [object] => stdClass Object
                (
                    [id] => in_00000000000000
                    [object] => invoice
                    [amount_due] => 1900
                    [application_fee] => 
                    [attempt_count] => 1
                    [attempted] => 1
                    [charge] => _00000000000000
                    [closed] => 1
                    [currency] => gbp
                    [customer] => cus_00000000000000
                    [date] => 1449067309
                    [description] => 
                    [discount] => 
                    [ending_balance] => 0
                    [forgiven] => 
                    [lines] => stdClass Object
                        (
                            [data] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => sub_7SYJYTaAqgniTB
                                            [object] => line_item
                                            [amount] => 1900
                                            [currency] => gbp
                                            [description] => 
                                            [discountable] => 1
                                            [livemode] => 1
                                            [metadata] => stdClass Object
                                                (
                                                )

                                            [period] => stdClass Object
                                                (
                                                    [start] => 1451751114
                                                    [end] => 1454429514
                                                )

                                            [plan] => stdClass Object
                                                (
                                                    [id] => email@email.com
                                                    [object] => plan
                                                    [amount] => 1900
                                                    [created] => 1449067298
                                                    [currency] => gbp
                                                    [interval] => month
                                                    [interval_count] => 1
                                                    [livemode] => 
                                                    [metadata] => stdClass Object
                                                        (
                                                        )

                                                    [name] => email@email.com
                                                    [statement_descriptor] => 
                                                    [trial_period_days] => 
                                                )

                                            [proration] => 
                                            [quantity] => 1
                                            [subscription] => 
                                            [type] => subscription
                                        )

                                )

                            [total_count] => 1
                            [object] => list
                            [url] => /v1/invoices/in_17DbnlFMZc35g8A2r4s2DYmb/lines
                        )

                    [livemode] => 
                    [metadata] => stdClass Object
                        (
                        )

                    [next_payment_attempt] => 
                    [paid] => 1
                    [period_end] => 1449067309
                    [period_start] => 1449067309
                    [receipt_number] => 
                    [starting_balance] => 0
                    [statement_descriptor] => 
                    [subscription] => sub_00000000000000
                    [subtotal] => 1900
                    [tax] => 
                    [tax_percent] => 
                    [total] => 1900
                    [webhooks_delivered_at] => 1449067325
                )

        )

)

第二次编辑:

我已经尝试了这一点但仍然没有:

$user = $event_json->data->object->lines->data->plan["id"];

还试过这个:

$user = $event_json->data->object->lines->data["plan"]->id;

而且:

$user = $event_json->data->object->lines->data["id"];

0 个答案:

没有答案