質問

I am trying to get some data from my MySQL server, but german umlauts are converted to its unicode-specific code.

header("Content-type: application/json; charset=utf-8"); 
$mysqli = new mysqli('host', 'user', 'pwd', 'databasename');
$mysqli->set_charset("utf8");

$result = $mysqli->query("SELECT Name FROM Table");
SQLtoJSON($result);


function SQLtoJSON($ab) {
    $tempArray = array();
    $myArray = array();
    while($row = $ab->fetch_object()) {
        $tempArray = $row;
        array_push($myArray, $tempArray);
    }
    echo json_encode($myArray);
}

The output:

[{"Name":"Frische-B\u00e4ckerei-Huber"}]

It should be:

[{"Name":"Frische-Bäckerei-Huber"}]

I also tried $mysqli->query("SET NAMES 'UTF8'"), but it didn't work. The server's charset is UTF-8.

EDIT: It's definitely a problem with json, var_dump() has the correct text.

役に立ちましたか?

解決

The problem is not in MySQL. Actually, the fact that you get that unicode notation proves that the data is in fact interpreted as a UTF-8 character (and probably the right character as well ;)).

The problem is in encoding it to json. By default json_encode escapes those characters.

That is not wrong by the way. You are allowed to escape them, but JSON also allows UTF-8 characters to be unescaped. JSON isn't meant to be human readable per se. It's just a format to send data over the line. The receiving party should be able to unescape the unicode sequence and show it as a ü.

Anyway, since JSON can also contain unescaped characters, you don't need to escape them, and you can tell json_encode not to by passing a flag:

json_encode($myArray, JSON_UNESCAPED_UNICODE);

See json_encode function and json constants for more details.

他のヒント

json_encode makes every ä to \u00e4. So i think the output is how it should be. just not the way you want ;)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top