Accent error in json with php

Asked

Viewed 23 times

0

Can you help me? I have a code that looks like this:

$var = "Paraná";
echo $var;
$json = json_encode($var);
echo $json;

In the echo of $var, returns me: Paraná. In the echo of $json returns me: Paran u00e1.

The same problem occurs for any other "special" character (à õ í é ü ç etc).

I’ve tried: ---- header("Content-type: text/html; charset=utf-8"); ---- utf8_encode($json); ---- utf8_decode($json); ---- < meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>;

Nothing corrects. Does anyone know a solution? Thank you!!

  • This is called escape, if you are using json_encode in string then it is because you intend to pass it to a json, or not? So it is no codec problem, no problem at all, the application that will use the response JSON will adjust alone, for example if it is an Ajax answer.... now if you are using json_encode and do not intend to work with JSON then this you did in the code makes no sense.

  • I understand. So it’s my own mistake. Thanks for the info! I would just like to mark your reply as the best, but as a comment does not enable.

  • {"a":" u00e1"}and {"a":"á"}are different ways to write the same JSON document. The JSON decoder will decode the Unicode escape. From php 5.4+ you can use: $json = json_encode($var, JSON_UNESCAPED_UNICODE); that will print right.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.