PHP中JSON到UTF-8的转换问题


Converting JSON to UTF-8 issues in PHP

所以我有这个程序,它允许用户在表单中输入信息,并在提交时将信息转换为JSON文件。当用户转到程序的不同部分时,程序会检索JSON文件,并从中构建一个调查问卷

JSON文件的构建工作正常,但每当我尝试检索文件时,我都会收到一个错误,即JSON以ASCII和NULL返回。我已经做了功课,发现这通常发生在编码冲突时(尽管ASCII是UTF-8的子集…)

因此,我确保在创建我使用的文件时使用mb_convert_encoding($x, 'UTF-8', 'auto');以确保JSON被正确地编码为UTF-8。

在检索JSON时,我也使用了mb_convert_encoding,但发现双重编码可能会导致问题,所以当我删除该部分时,它不再响应编码(使用mb_detect_encodiing),但它仍然为NULL。

我甚至拉下JSON文件,将其保存为UTF-8并重新上传

非常感谢在这方面的任何帮助。我已经为此头疼了两天了。这是内置在代码点火器中的,如果这对有影响的话

以下是创建JSON文件的代码:

        $thisClient = $this->input->cookie('client');
$date = "%m-%Y";
$date = mdate($date);
$clientDir = *********PATH TO CREATE THE DIRECTORIES IN;
$dialogDir = $clientDir."/".$date;
$d_file_name = $thisClient.'-'.$date;

//check to see if client directory exists, if it doesn't then it creates it 
if(!is_dir($clientDir)){
    mkdir($clientDir, 0755, TRUE);  
    echo "Client Directory Created!<br>";
} else{
    echo "No Client Directory Created!<br>";
}

//check to see if client directory exists, if it doesn't then it creates it 
if(!is_dir($dialogDir)){
    mkdir($dialogDir, 0755, TRUE);  
    echo "DIALOG Directory Created!<br>";
} else{
    echo "No DIALOG Directory Created!<br>";
}
$custDialog = array();

if(isset($_POST['cust-dialog-title'])){
    function encodeMe($x){
        //this ensure proper encoding
        return mb_convert_encoding($x, 'UTF-8', 'auto');
    }
    $customDialog = array();
    for($i = 0; $i < count($_POST['cust-dialog-title']); $i++){
        $customDialog[$i]["title"] = encodeMe($_POST['cust-dialog-title'][$i]);
        $customDialog[$i]["intro"] = encodeMe($_POST['cust-dialog-intro'][$i]);

            for($ii = 0; $ii < count($_POST['cust-dialog-quest-'.$i]); $ii++){
                $customDialog[$i]["questions"]["q".$ii] = encodeMe($_POST['cust-dialog-quest-'.$i][$ii]);

                if($_POST["cust-dialog-pos-".$i."-".$ii] == "TRUE"){
                    //if the question is a true positive
                    $customDialog[$i]["questions"]["agree"] = -5;
                    $customDialog[$i]["questions"]["disagree"] = 5;
                } else{
                    //if the question is a false positive
                    $customDialog[$i]["questions"]["agree"] = 5;
                    $customDialog[$i]["questions"]["disagree"] = -5;
                }
            }
            $jsonDIALOG = json_encode($customDialog);
            $jsonDIALOG = str_replace("[", " ", str_replace("]", " ", $jsonDIALOG));

            if ( ! write_file($dialogDir."/".$d_file_name.".json", $jsonDIALOG )) {
                     echo 'Unable to write the file';
                } else {
                     echo 'File written!';
                }
            //save Custom DIALOG info in database
            ***********DATABASE INFO**************

    }

}

以下是检索JSON对象的代码:

if($row["custom"] !== null){ //If the Dialog is a Custom Dialog

        $path = str_replace(*****removes an unnecessary portion from the path string**);
            $thisDialog = file_get_contents(****PATH TO JSON FILES*****);
            //THE FOLLOWING helps debug issues with the JSON -- displays order number and dialog being called -- uncomment to use
            //echo $i.' is '.$curDialog[$i]. '<br>';  
            //$thisDialog = substr($thisDialog,1);
            //echo $thisDialog;
            //THIS IS THE CODE FOR DEBUGGING ENCODING ISSUES
            //$thisDialog = mb_convert_encoding($thisDialog, 'UTF-8', 'ASCII');
            //echo mb_detect_encoding($thisDialog);

            $jsonDialog = json_decode($thisDialog, true);
            echo var_dump($jsonDialog); 

            if($jsonDialog){
                $allDialogs = $jsonDialog;
            } else { 
                echo "Error: Invalid Dialog. Call Order# 0<br>" ;
            }
                return $allDialogs;

    }

我已经包含了一些调试内容,这些内容我已经尝试过并评论过了。谢谢

您可能应该将JSON_UNESCAPED_UNCODE添加为json_encode的一个选项。请记住,此常量自PHP 5.4.0

起就可用