简体   繁体   中英

Json_decode is returning null with proper Json input

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

}

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
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM