简体   繁体   中英

Converting JSON to UTF-8 issues in PHP

So I have this program that allows a user to enter information into a form and upon submission turns that information into a JSON file. When a user goes to a different part of the program, the programs retrieves the JSON file and builds a questionnaire out of it.

The building of the JSON file works fine but whenever I try to retrieve the file I'm getting an error that the JSON is returning as ASCII and as NULL. I've done my homework and saw that this usually happens when their is an encoding conflict(even though ASCII is a subset of UTF-8...).

So I made sure that when creating the file I'm using using mb_convert_encoding($x, 'UTF-8', 'auto'); to ensure that the JSON is properly being encoded as UTF-8.

I was also using mb_convert_encoding when retrieving the JSON, but saw that double encoding can cause issues so when I removed that piece it no longer echoed out what the encoding was(using mb_detect_encoding) but it is still NULL.

I even went so far as to pull down the JSON file, save it as UTF-8 and re-upload it.

Any and all help on this is much appreciated it. I've banged my head for two days over this. This is built in Code Ignitor, if that makes a difference

Here is the code to create the JSON file:

        $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**************



    }



}

Here is the code to retrieve the JSON object:

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;


    }

I've included some debugging things that I've tried and commented out. Thanks!!

You should probably add JSON_UNESCAPED_UNICODE as an option to json_encode . Keep in mind that this constant is available since PHP 5.4.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM