bodruk
bodruk

Reputation: 3262

Special chars with json_encode()

I'm trying the following code:

header('Content-type: application/json; charset=UTF-8');
$val = array('code' => 1, 'message' => 'Não encontrado!');
$res = json_encode( $val );
echo $res;

The answer is:

{ "code" : 1, "message" : "N\u00e3o encontrado!" }

I searched, but I couldn't find an aswer to this problem. I think it's an UTF-8 encode issue. My source file is encoded with UTF-8. Any idea?

Upvotes: 0

Views: 151

Answers (1)

Ry-
Ry-

Reputation: 224857

That’s not a problem. \u00e3 represents ã in JSON.

js> 'ã'.charCodeAt(0).toString(16)
"e3"

If you really need it, though, you can tell PHP not to escape it:

$res = json_encode($val, JSON_UNESCAPED_UNICODE);

Hooray!

Upvotes: 6

Related Questions