douwaz34842 2013-12-13 12:43
浏览 270
已采纳

Json_decode使用适当的Json输入返回null

So I have this 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"}] 

Which could be properly processed through : Online Json Viewer

Problem is that when I am trying to decode this string using json_decode function like:

$decodedJson = json_decode($jsonString);
var_dump($decodedJson);

The result I get is NULL

Can anyone point me out what am I missing?

Edit (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);

}
  • 写回答

1条回答 默认 最新

  • doumi1944 2013-12-13 12:45
    关注

    I am getting yours perfectly right. Maybe you didn't enclose your $jsonString under proper quotes.

    <?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
    

    OUTPUT :

    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
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services