PHP output JSON Web Service charset UTF-8 error

encoding, hebrew, json, php, web-services

Solution

If your db-field is utf8 you should fist do:

mysql_query("SET NAMES 'utf8'");

You should always do the 'SET NAMES...' before inserting your data, too. Be sure that you really stored utf8 encoded strings!

then do your query:

mysql_query($your_query);

$array = array("tasklist"=>array());    

while($row = mysql_fetch_array($queryExe)){
        $a = array();
        $a["customerID"] = $row['AUTO_ID'];
        $a["name"] = $row['Customer_Name'];
        $a["cargo"] = $row['Type_of_Cargo'];
        $a["destination"] = $row['Destination'];
        $a["quantity"] = $row['Quantity'];
        $a["startdate"] = $row['startdate'];
        $array["tasklist"][] = $a;
}

header("Content-type: application/json; charset=utf-8");

echo json_encode($array);
exit();

i've made the experience that these is not enough when the servers default charset is for example iso. In that case i need to do the following in my .htaccess:

AddDefaultCharset utf-8

Problem

I am hosting a web service in JSON output by PHP. I have Hebrew data set in DB and I am posting this as an output to Web service. When I post the data initially it output the result as follows: JSON: ``` { "tasklist": [ { "customerID": "9936", "name": "טר ×רמה ×™×–×•× ×•×‘×™× ×•×™ בע"מ", "cargo":"×ברר", "destination":"מכר", "quantity":"1.000000", "startdate":"03/01/201300: 00: 00" } ] } ``` But this `"×ברר"` can be readable by Android/Iphone parser and convert it to original Hebrew. But i faced Error in `"name": "טר ×רמה ×™×–×•× ×•×‘×™× ×•×™ בע"מ",`. where `"` is in between the string so the JSON is not valid and shows error! To Over come this issue I used `UTF-8` to convert `"×ברר"` this to Hebrew `"נברר"`. But in this case too the problem remains same: PHP: header('Content-type: text/html; charset=UTF-8'); JSON: ``` { "tasklist": [ { "customerID": "9936", "name": "טר ארמה יזום ובינוי בע"מ", "cargo":"נברר", "destination":"מכר", "quantity":"1.000000", "startdate":"03/01/201300: 00: 00" } ] } ``` But still the problem remains: Also in some case I am getting this `�` because of using UTF-8 ``` "name":"מחצבות כפר גלעדי-חומרי מ�" ``` - How can I overcome this issue? - Is there any other specific encode I need to use? Note: The data cannot be changes in Database The solution should be while output to JSON. How the data stored in DB is shown below: name מחצבות כפר גלעדי-חומרי ×ž× My PHP Script which output JSON: ``` <?php //My DB connection and Query comes here $jsontext = '{"tasklist":['; while($row = mysql_fetch_array($queryExe)){ $jsontext .= '{"customerID":"'.$row['AUTO_ID'].'",'; $jsontext .='"name":"'.$row['Customer_Name'].'",'; $jsontext .='"cargo":"'.$row['Type_of_Cargo'].'",'; $jsontext .='"destination":"'.$row['Destination'].'",'; $jsontext .='"quantity":"'.$row['Quantity'].'",'; $jsontext .='"startdate":"'.$row['startdate'].'"},'; } $jsontext = substr_replace($jsontext, '', -1); // to get rid of extra comma $jsontext .= "]}"; header('Content-type: text/html; charset=UTF-8'); //Output the final JSON echo $jsontext; ?> ``` Thank you for your help in advance! Was the question clear? to understand my issue.

Original source