5
4

3 回答 3

15

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
于 2013-08-02T05:12:53.293 回答
1

You should change your code to use json_encode. You need to pass it properly utf8 encoded data.

If you are using MySQL you can try running the following before your query to get your data.

SET NAMES 'utf8';

You can also look into using utf8_encode.

于 2013-08-02T05:13:19.927 回答
1

From http://www.php.net/manual/en/function.json-encode.php#100565

That said, quotes " will produce invalid JSON, but this is only an issue if you're using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.

May be you can replace " with \" , i guess it will solve the issue.

Source : PHP JSON String, escape Double Quotes for JS output

于 2013-08-02T15:28:38.587 回答