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

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题