Good day.

Json answer in the "wrong encoding" displays Russian characters. If the characters are English, then everything is OK, and if the Cyrillic alphabet, then this is the form:

[{"Id":"1","Fio":"Rakzin Roman"},{"Id":"2","Fio":"\u0418\u0432\u0430\u043d\u043e\u0432 \u0418\u0432\u0430\u043d \u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447"},{"Id":"3","Fio":"\u041f\u0435\u0442\u0440\u043e\u0432 \u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432\u0438\u0447"},........ 

Php:

 echo json_encode($this->Vipolnit_Zapros("Select Id,Fio from Students")); 

Tell me what to do. How to remove such characters here: \u0418 ? Although later everything is well displayed in JavaScript, but I would like to remove it.

Thank.

    3 answers 3

    How to remove such characters here: \ u0418? Although later everything is well displayed in JavaScript, but I would like to remove it.

    To remove them, use the second parameter of the json_encode() function:

     json_encode($data, JSON_UNESCAPED_UNICODE); 

    JSON_UNESCAPED_UNICODE - Do not encode multibyte Unicode characters (by default, they are encoded as \ uXXXX). Available since PHP 5.4.0.

      Everything is normal, "\u0418\u0432\u0430\u043d\u043e\u0432 \u0418\u0432\u0430\u043d \u0418\u0432\u0430\u043d\u043e\u0432\u0438\u0447" is not "encoding", but " Unicode character codes "written in eight-bit encoding, the text "Иванов Иван Иванович" more precisely written down.

      In json, data is written to ASCII 0-127. Anything that does not fit is written as Unicode codes ( "\u0418\u0432\u0430\u043d\u043e\u0432" this is "Иванов" ).

      • this is not an "eight-bit encoding", but ordinary hexadecimal numbers, for example, 447 is 1095 in the decimal system, which corresponds to the "ч" character in Unicode. Not mandatory to encode json-text using ascii, for example, you can use utf-8, utf-16be. - jfs
      • @jfs 1. just eight-bit ASCII 0-127 encoding, (do not confuse "encoding" and "character and code table") 2. and this is bad advice from the category:> It is not necessary to encode json-text,> using ascii, for example, you can> use utf-8, utf-16be. in this case, you can get problems with the character encoding, it is because of this problem that ASCII 0-127 de facto used in JSON. Of course, if everything is set up correctly and everywhere, then there will be no problem, but it is much easier to use "Unicode character codes" - ProkletyiPirat
      • 2
        complete nonsense. The json format is defined for Unicode text. For example: "\u0418" is a json string that can be encoded into bytes using different encodings. Most often this is utf-8, but depending on the application, other encodings may be more preferable. Using only characters that can be represented in ascii encoding is a bad method for fixing an application that does not work correctly with encodings. - jfs
      • quietly let specs - etki
      • Here the same information, only set out 10 times shorter. - avp

      Any character in a json string can be represented as a sequence of \uxxxx even English characters, for example, "V" corresponds to "\u0056" . Non-BMP characters (outside U+0000-U+FFFF ) can be represented as a surrogate UTF-16 pair (such as "\ud83c\udf82" - corresponding to the symbol U + 1F382 BIRTHDAY CAKE ). Hexadecimal digits xxxx encode the symbol number (Unicode codepoint).

      JSON format is defined for Unicode text. When writing to a file, transferring over a network or another byte interface, it may be necessary to encode this text as a sequence of bytes using utf-8, utf-16, or utf-32 encodings (encodings recommended by rfc 7159 ).

      \uxxxx sequences allow you to save json-text using ascii encoding. To leave the characters as they are, JSON_UNESCAPED_UNICODE can be used as @mantigatos suggested .

      "V" and "\u0056" json strings are decoded by json_decode() into the same character .