簡體   English   中英

在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起可用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM