lundi 31 août 2015

Improve my parsing of JSON in PHP

Right now, I am currently parsing this JSON in a way that I feel can be improved upon. I am new at JSON, but I would expect there to be a better way of doing it than what I am currently doing.

Here is the JSON that is returned to me from Stripe's API (sensitive data removed):

Stripe\Customer JSON: { "id": "cus_6smHAG99OncrSm", "object": "customer", "created": 1440821625, "livemode": false, "description": null, "email": null, "shipping": null, "delinquent": false, "metadata": [], "subscriptions": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/subscriptions", "data": [ { "id": "sub_6smHbsmTA6JhrP", "plan": { "id": "yearly", "interval": "year", "name": "yearly", "created": 1439229255, "amount": 5000, "currency": "usd", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 7, "metadata": [], "statement_descriptor": "yearly" }, "object": "subscription", "start": 1440821627, "status": "trialing", "customer": "cus_6smHAG99OncrSm", "cancel_at_period_end": false, "current_period_start": 1440821627, "current_period_end": 1441426427, "ended_at": null, "trial_start": 1440821627, "trial_end": 1441426427, "canceled_at": null, "quantity": 1, "application_fee_percent": null, "discount": null, "tax_percent": null, "metadata": [] } ] }, "discount": null, "account_balance": 0, "currency": "usd", "sources": { "object": "list", "total_count": 1, "has_more": false, "url": "\/v1\/customers\/cus_6smHAG99OncrSm\/sources", "data": [ { "id": "card_16f0ioKTaUqqkEkT7osN0knY", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 10, "exp_year": 2017, "fingerprint": "Y1GhThZ3NCmHOdTv", "country": "US", "name": "blah@yahoo.com", "address_line1": null, "address_line2": null, "address_city": null, "address_state": null, "address_zip": null, "address_country": null, "cvc_check": null, "address_line1_check": null, "address_zip_check": null, "tokenization_method": null, "dynamic_last4": null, "metadata": [], "customer": "cus_6smHAG99OncrSm" } ] }, "default_source": "card_16f0ioKTaUqqkEkT7osN0knY" }

The main problem I am having is that I have to convert the above into a string manually and then remove the prefix (which is the "Stripe\Customer JSON:" part). Here is what I'm currently doing to parse it:

$json = strval(\Stripe\Customer::retrieve($user->stripe_id));

//Trim the prefix
$json = substr($json, 21);
$data = json_decode($json, true);
$current_period_end = $data['subscriptions']['data'][0]['current_period_end'];

The reason why I think there is a better way to do this is because I find it hard to believe that every time I receive JSON I need to convert it to a String and then find out if there is a prefix and then cut the prefix off before I can turn it into an associative array so I can use it.

Please tell me there is a better way and show me how to do it. Thank you.

On a side note, I am using Laravel, if there are any relevant features to benefit from therein I'm all ears.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1LPPa9K
via IFTTT

Aucun commentaire:

Enregistrer un commentaire