在 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_encoding) 但它仍然是 NULL。

我什至把JSON文件下载下来,另存为UTF-8再重新上传。

非常感谢对此提供的任何和所有帮助。我已经为此伤脑筋两天了。这是内置在 Code Ignitor 中的,如果有区别的话

这是创建 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_UNICODE 作为 json_encode 的选项。请记住,此常量自 PHP 5.4.0

起可用