如何从var_export访问条带json对象

时间:2015-07-11 21:58:57

标签: php json

我有一个来自Stripe的大型JSON对象,我正在尝试获取客户字段数据。

数据似乎是未序列化的,$ result [16];将访问第17个字符空间而不是元素16(只是试图获得任意输出)。 我尝试将json_decode放在几个地方,这会阻止数据命中我的变量。

我在想var_export会破坏编码,但我不确定如何将其恢复为可行的格式。

代码:

$request = file_get_contents('php://input');
$array = json_decode($request,true);
$result2 = $array;
$file = 'stripedump.txt';
$current = file_get_contents($file);
$current .= $result2;
file_put_contents($file, $current);

JSON对象:

{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "invoice.created",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2013-02-13",
  "data": {
    "object": {
      "date": 1436647384,
      "id": "in_00000000000000",
      "period_start": 1436647384,
      "period_end": 1436647384,
      "lines": {
        "data": [
          {
            "id": "sub_6agAX0pPxijkpq",
            "object": "line_item",
            "type": "subscription",
            "livemode": true,
            "amount": 0,
            "currency": "usd",
            "proration": false,
            "period": {
              "start": 1436647384,
              "end": 1439239384
            },
            "subscription": null,
            "quantity": 1,
            "plan": {
              "interval": "month",
              "name": "plan1",
              "created": 1435896032,
              "amount": 749,
              "currency": "usd",
              "id": "1",
              "object": "plan",
              "livemode": false,
              "interval_count": 1,
              "trial_period_days": 30,
              "metadata": {
              },
              "statement_descriptor": "Ex-Mailer.com",
              "statement_description": "Ex-Mailer.com"
            },
            "description": null,
            "discountable": true,
            "metadata": {
            }
          }
        ],
        "total_count": 1,
        "object": "list",
        "url": "/v1/invoices/in_6agAuGmTHHa8Qi/lines"
      },
      "subtotal": 0,
      "total": 0,
      "customer": "cus_00000000000000",
      "object": "invoice",
      "attempted": false,
      "closed": false,
      "forgiven": false,
      "paid": true,
      "livemode": false,
      "attempt_count": 0,
      "amount_due": 0,
      "currency": "usd",
      "starting_balance": 0,
      "ending_balance": null,
      "next_payment_attempt": 1436650984,
      "webhooks_delivered_at": null,
      "charge": null,
      "discount": null,
      "application_fee": null,
      "subscription": null,
      "tax_percent": null,
      "tax": null,
      "metadata": {
      },
      "statement_descriptor": null,
      "description": null,
      "receipt_number": null,
      "statement_description": null
    }
  }
}
用于启动请求的

命令:

gentoo-mini htdocs # curl -i -X POST -H "Content-Type: application/json" -d "{    "created": 1326853478,    "livemode": false,    "id": "evt_00000000000000",    "type": "invoice.created",    "object": "event",    "request": null,    "pending_webhooks": 1,    "api_version": "2013-02-13",    "data": {      "object": {        "date": 1436647384,        "id": "in_00000000000000",        "period_start": 1436647384,        "period_end": 1436647384,        "lines": {          "data": [            {              "id": "sub_6agAX0pPxijkpq",              "object": "line_item",              "type": "subscription",              "livemode": true,              "amount": 0,              "currency": "usd",              "proration": false,              "period": {                "start": 1436647384,                "end": 1439239384              },              "subscription": null,              "quantity": 1,              "plan": {                "interval": "month",                "name": "plan1",                "created": 1435896032,                "amount": 749,                "currency": "usd",                "id": "1",                "object": "plan",                "livemode": false,                "interval_count": 1,                "trial_period_days": 30,                "metadata": {                },                "statement_descriptor": "Ex-Mailer.com",                "statement_description": "Ex-Mailer.com"              },              "description": null,              "discountable": true,              "metadata": {              }            }          ],          "total_count": 1,          "object": "list",          "url": "/v1/invoices/in_6agAuGmTHHa8Qi/lines"        },        "subtotal": 0,        "total": 0,        "customer": "cus_00000000000000",        "object": "invoice",        "attempted": false,        "closed": false,        "forgiven": false,        "paid": true,        "livemode": false,        "attempt_count": 0,        "amount_due": 0,        "currency": "usd",        "starting_balance": 0,        "ending_balance": null,        "next_payment_attempt": 1436650984,        "webhooks_delivered_at": null,        "charge": null,        "discount": null,        "application_fee": null,        "subscription": null,        "tax_percent": null,        "tax": null,        "metadata": {        },        "statement_descriptor": null,        "description": null,        "receipt_number": null,        "statement_description": null      }    }  }" http://nyctelecomm.com/hooker/
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Sat, 11 Jul 2015 23:33:46 GMT
Server: Apache/2.4.12 (FreeBSD) PHP/5.6.10
X-Powered-By: PHP/5.6.10
Content-Length: 0
Content-Type: text/html; charset=UTF-8

如何访问此JSON对象中的'customer:'元素?

1 个答案:

答案 0 :(得分:3)

你试过从json字符串返回一个关联数组吗?

$array = json_decode($request,true); // return assoc array
echo $array["data"]["object"]["customer"];

var_exportvar_dump非常相似,两个函数都创建了给定变量的表示。 在这种情况下,无需使用var_export

PHP:var_export

PHP:json_decode