所以我有这个json:
[{"id":"1","url":"https:\/\/someurl.com\/","starttime":"2010-11-30 16:14:12","finishtime":"2011-09-11 11:32:31","aborted":"0"},{"id":"2","starturl":"http:\/\/someurl.com\/","starttime":"2010-11-30 16:14:12","finishtime":"2013-10-15 14:49:16","aborted":"0"},{"id":"5","starturl":"https:\/\/someurl.com\/","starttime":"2010-11-30 16:14:12","finishtime":"2013-10-17 04:15:58","aborted":"0"}]
可以通过以下方式正确处理:Online Json Viewer
问题在于,当我尝试使用json_decode
函数解码此字符串时,如:
$decodedJson = json_decode($jsonString);
var_dump($decodedJson);
我得到的结果是NULL
有人能指出我错过了什么吗?
编辑(fullScript)
try {
$ch = curl_init();
$username ='u';
$password='p';
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL,"https://someinternallink");
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_PROXY, 'someinternalproxy');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_CAPATH , 'pathtorootca');
curl_setopt($ch, CURLOPT_ENCODING, 'compress, gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS,'[1]');
$content = curl_exec($ch);
if (FALSE === $content)
throw new Exception(curl_error($ch), curl_errno($ch));
$rawResponse = htmlentities($content);
//$rawResponse variable is 100% identical like I have posted above
try{
print_r(json_decode(trim($rawResponse)));
}
catch (Exception $e){
echo $e;
}
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),E_USER_ERROR);
}
答案 0 :(得分:0)
我让你完全正确。也许您没有将 $jsonString
括在适当的引号下。
<?php
$arr = '[{"id":"1","url":"https:\/\/someurl.com\/","starttime":"2010-11-30 16:14:12","finishtime":"2011-09-11 11:32:31","aborted":"0"},{"id":"2","starturl":"http:\/\/someurl.com\/","starttime":"2010-11-30 16:14:12","finishtime":"2013-10-15 14:49:16","aborted":"0"},{"id":"5","starturl":"https:\/\/someurl.com\/","starttime":"2010-11-30 16:14:12","finishtime":"2013-10-17 04:15:58","aborted":"0"}]';
print_r(json_decode($arr,true)); //Used a true flag .. Nothing else
输出:
Array
(
[0] => Array
(
[id] => 1
[url] => https://someurl.com/
[starttime] => 2010-11-30 16:14:12
[finishtime] => 2011-09-11 11:32:31
[aborted] => 0
)
[1] => Array
(
[id] => 2
[starturl] => http://someurl.com/
[starttime] => 2010-11-30 16:14:12
[finishtime] => 2013-10-15 14:49:16
[aborted] => 0
)
[2] => Array
(
[id] => 5
[starturl] => https://someurl.com/
[starttime] => 2010-11-30 16:14:12
[finishtime] => 2013-10-17 04:15:58
[aborted] => 0
)
)