2014-10-27 2 views
-2

Это мой ответ json, но когда я пытаюсь декодировать строку Json, я получил ошибку NULL. Когда я исправляю один и тот же выход json в онлайн-декодировании, он работает правильно. В чем проблема? Я не могу исправить ошибку.Попытка разобрать JSON с использованием PHP

{ 
    "amount": 474, 
    "created": 1414385307, 
    "currency": "usd", 
    "id": "-snip-", 
    "livemode": false, 
    "paid": true, 
    "refunded": false, 
    "disputed": null, 
    "captured": true, 
    "description": null, 
    "statement_description": null, 
    "failure_message": null, 
    "failure_code": null, 
    "amount_refunded": 0, 
    "customer": "-snip-", 
    "invoice": null, 
    "refunds": { 
    "data": [], 
    "total_count": 0, 
    "has_more": false, 
    "url": "/v1/charges/-snip-/refunds", 
    "count": null 
    }, 
    "card": { 
    "exp_month": 11, 
    "exp_year": 2025, 
    "last4": "-snip-", 
    "country": "US", 
    "type": null, 
    "name": null, 
    "id": "-snip-", 
    "customer": "-snip-", 
    "recipient": null, 
    "address_line1": null, 
    "address_line2": null, 
    "address_zip": null, 
    "address_city": null, 
    "address_state": null, 
    "address_country": null, 
    "address_zip_check": null, 
    "address_line1_check": null, 
    "cvc_check": null, 
    "fingerprint": "-snip-", 
    "brand": "Visa", 
    "funding": "credit" 
    }, 
    "dispute": null, 
    "balance_transaction": "-snip-", 
    "metadata": {} 
} 

код PHP:

$curl = curl_init($service_url); 
$curl_post_data = array(
     'customerId' => 'Cus_id', 
     'amount' => 474 
); 
$data = json_encode($curl_post_data); 

$headers = array('Content-type: application/json'); 

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$curl_response = curl_exec($curl); 
$json=str_replace("JSON: ","","$curl_response"); 
var_dump(json_decode($json)); 
var_dump(json_decode($json, true)); 
+0

Вы, вероятно, должны быть обеспокоены тем, что 'id' находится там; это общедоступный сайт. – Qix

+3

Пожалуйста, введите ваш код – Gunaseelan

+1

Как вы используете 'json_decode'? Вы используете флаг 'Associative Array'' (json_decode (array, flag) '? –

ответ

2

Pass что вся строка json_decode() и он будет работать.

0

Попробуйте один

$str = '{ "amount": 474, "created": 1414385307, "currency": "usd", "id": "-snip-", "livemode": false, "paid": true, "refunded": false, "disputed": null, "captured": true, "description": null, "statement_description": null, "failure_message": null, "failure_code": null, "amount_refunded": 0, "customer": "-snip-", "invoice": null, "refunds": { "data": [], "total_count": 0, "has_more": false, "url": "/v1/charges/-snip-/refunds", "count": null }, "card": { "exp_month": 11, "exp_year": 2025, "last4": "-snip-", "country": "US", "type": null, "name": null, "id": "-snip-", "customer": "-snip-", "recipient": null, "address_line1": null, "address_line2": null, "address_zip": null, "address_city": null, "address_state": null, "address_country": null, "address_zip_check": null, "address_line1_check": null, "cvc_check": null, "fingerprint": "-snip-", "brand": "Visa", "funding": "credit" }, "dispute": null, "balance_transaction": "-snip-", "metadata": {} }'; 
$data = json_decode($str); 
print_r($data); 
+1

Он работает, но когда я передаю переменную, я получил пустой экран в своем браузере @Manisha Patel –