String appears to be valid JSON, but `json_decode()` returns NULL

前端 未结 5 1102
感动是毒
感动是毒 2020-12-19 08:31

I think I\'ve found where the error lies:

    $convertJSON = file_get_contents(\"http://www.google.com/ig/calculator?hl=en&q=\" . $currencyValue . $curre         


        
相关标签:
5条回答
  • 2020-12-19 08:55

    Not a direct response to this question, but an issue I spent a few hours trying to resolve.

    If you are attempting to decode JSON that came from a remote file via CURL, and if that file is in UTF-8 format, the beginning of the file may have the following characters (which breaks json_decode():

    
    

    Which you will not see with the naked eye, only via htmlentities(); I have no idea why they are there, I traced this all the way to curl_exec(), thinking that maybe they were being added there. In any case, those little bastards were being added only when file is in UTF-8 format.

    So, assuming you have no control over the encoding of the source file, you can do something like this before passing the string into json_decode():

    $encoding = mb_detect_encoding($json);
    
    if($encoding == 'UTF-8') {
      $json = preg_replace('/[^(\x20-\x7F)]*/','', $json);    
    }    
    
    print_r(json_decode($json));
    

    I hope I save somebody some time, it took me a few hours of tracing to figure out that's what was happening.

    0 讨论(0)
  • 2020-12-19 08:55

    to make json_encode workable, you have to add double quote to the result string in order to make it in JSON format.

    I tries the simple code below , and it works fine:

    $data = '{lhs: "1 U.S. dollar",rhs: "7.80177256 Hong Kong dollars",error: "",icc: true}';
    $data = str_replace('lhs','"lhs"',$data);
    $data = str_replace('rhs','"rhs"',$data);
    $data = str_replace('error','"error"',$data);
    $data = str_replace('icc','"icc"',$data);
    print_r(json_decode($data));
    

    Result:

    stdClass Object ( [lhs] => 1 U.S. dollar [rhs] => 7.80177256 Hong Kong dollars [error] => [icc] => 1 ) 
    

    Now it is in json_decode object!

    0 讨论(0)
  • 2020-12-19 08:58

    Expanding on Matthews answer:

    In case for some reason you want all data; (probably not but just in case?)

    $c='{lhs: "1 British pound",rhs: "1.5358 U.S. dollars",error: "",icc: true}';
    $j=json_decode(preg_replace('/({|,)([a-z]+): /','$1"$2": ',$c));
    var_dump($j->{'rhs'});
    

    I stumbled across this page before I worked it out so maybe others will too :)

    0 讨论(0)
  • 2020-12-19 09:01

    The response Google is giving you isn't valid JSON because the labels are not quoted. You'll have to parse it yourself.

    $response = '{lhs: "555 Euros",rhs: "796.64700 U.S. dollars",error: "",icc: true';
    preg_match('/rhs:\s*"([^"]+)"/', $response, $m);
    echo $m[1];
    

    Output:

    796.64700 U.S. dollars
    
    0 讨论(0)
  • 2020-12-19 09:14

    strip it of everything but decimal points, commas, and numbers, and give me a result.

    Actually you do the exact contrary with your regex. Add a ^ after [: [^ to negate it

    $currencyValue = preg_replace('/([^0-9\.,]+)/', '', $currencyValue);
    
    0 讨论(0)
提交回复
热议问题