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

前端 未结 5 1119
感动是毒
感动是毒 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

    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!

提交回复
热议问题