1

I'm developing a web application with Symfony and MySQL as database. My database creation syntax is as below:

CREATE DATABASE `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

The table which I need to know why is returning unexpected text is:

CREATE TABLE `elements` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL
)ENGINE=InnoDB AUTO_INCREMENT=132 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

And the entity code in PHP is:

/**
 * 
 *
 * @ORM\Table(name="elements")
 * @ORM\Entity()
 */
class Element
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;
}

When the name of the Element has Spanish characters like á,é,í,ó,ú,ñ; in the browser is correctly rendered, and in the database the characters are stored in this way, but when I try to return this information using a JsonResponse, the characters are converted to others codes. For example:

$result['id']       = $e->getId();
$result['name']     = $e->getName();

return new JsonResponse( array('response' => $result) );

And the response shows:

"response":{
    "id":26,
    "name":"Avi\u00f3n"
}

And name should be "Avión" instead.

I would like to know why this happens and how can avoid this behavior. Any ideas?

Thanks in advance.

2 Answers 2

2

I don't see actual problem here.

They are not converted to other codes.

Instead, they are utf-8 encoded (default JSON encoding).

Some info : RFC4627 section 2.5 strings

\u00F3 is utf-8 encoded character ó, see here

UPDATE:

Read this : JSON Encoding and decoding of UTF8 characters in PHP

Sign up to request clarification or add additional context in comments.

3 Comments

Just wanted to add this comment to accomplish your answer: To decode the encoded JSON in client side (e.g. jQuery) he might want to change the headers Content-Type to application/json; charset=utf-8, because that's what the client side is expecting for. It's always good to make things consistent. Right?
Thanks for the answer @rkosegi. I voted up your answer.
@Javad, agree with you, by explicitly setting encoding on client side you don;t break anything, but makes think more compliant.
1

When you called new JsonResponse($data), among other things, your data is encoded in this way:

json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);

By default, json_encode escapes unicode characters, so the output you get is the expected one. If you don't want to have your unicode escaped, you can create your controller response as follow:

$data = array('response' => $result);
return new Response( json_encode($data, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT));

1 Comment

return new JsonResponse( json_encode(['message' => 'success','result'=>$result], JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT), Response::HTTP_OK, ['charset'=>'UTF-8','Access-Control-Allow-Origin'=>'*'], true); to have headers set content-type application/json and other custom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.