这是我的json响应,但是当我尝试解码Json字符串时,我收到了NULL错误。当我在在线解码中修复相同的json输出时,它可以正常工作。这里有什么问题?我无法解决错误。
{
"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 :(得分:2)
将整个字符串传递给json_decode()
,它会起作用。
答案 1 :(得分: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);