有效的json不起作用

时间:2016-04-30 14:33:33

标签: php json response

我正在尝试在PHP中解码JSON字符串,但不知怎的,json_decode不喜欢我的字符串,我认为它不是有效的json。对我来说非常奇怪的是,如果我手动将json响应放在一个变量中,它就可以工作了。如果我在浏览器中写出json响应,并且我写了变量的内容,两者都完全相同,如下所示:

{"id":455463,"Created":"2016-04-30T14:20:38.09","SenderCompanyName":"x","InvoiceNumber":"2555","PaymentDueDate":"2016-04-30T00:00:00","ToBePaidAmount":350.0000}

如果我查看网页来源,内容也完全相同。我也尝试过转换为UTF8,但没有变化。

你们通常如何调试这个,或者我忘记了什么?

代码:

// calling web service and saving json response in variable  
$json_response = CallAPI($method, $url, $json_request);  

// the response contain some unvalid character in the end, so i am removing it  
$json_response = substr($json_response, 0, strpos($json_response, "}"));  

// trying to decode it, IT PRINTS OUT NULL  
var_dump(json_decode($json_response, true));  



// copying the json response from the above and putting it into a variable  
$json_response = '{"id":455433,"Created":"2016-04-30T12:55:12.313","SenderCompanyName":"x","InvoiceNumber":"2525","PaymentDueDate":"2016-04-30T00:00:00","ToBePaidAmount":350.0000}';

// trying to decode it, IT PRINTS OUT THE RESULT SUCCESFULLY
var_dump(json_decode($json_response, true));

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php
$json = '{"id":455463,"Created":"2016-04-30T14:20:38.09","SenderCompanyName":"x","InvoiceNumber":"2555","PaymentDueDate":"2016-04-30T00:00:00","ToBePaidAmount":350.0000}';
var_dump(json_decode($json));
?>

答案 1 :(得分:0)

我终于找到了解决方案。 我忘记在我的CURL OPTIONS中添加这个:

curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1);

添加后,其工作正常