1st becomes 1 u00aa when I enter into json

Asked

Viewed 90 times

3

When I insert in JSON with codeigniter, when turning into string it turns 1\u00aa.

$t[0]["teste"] = "1ª";
$data[0]["algo"] = $t;

1 answer

2


Normal. PHP uses the format \u quatro-dígitos-hex to escape special characters (non-ASCII). This is valid JSON, see json.org

If you really want to avoid escape, you can use the option JSON_UNESCAPED_UNICODE:

php > // com a flag:
php > print(json_encode('ºªáéíóú',JSON_UNESCAPED_UNICODE));
"ºªáéíóú"

php > // sem a flag:
php > print(json_encode('ºªáéíóú'));
"\u00ba\u00aa\u00e1\u00e9\u00ed\u00f3\u00fa"
  • And there’s some way to save how ª within JSON, or it only allows for character code ??

  • You’re already saving the ª, it is only being represented in hexadecimal. When you consume JSON, values are typically decoded. For example: type '\u00aa' and enter the Devtools console of your browser. It will be displayed ª.

  • You can send a th directly to JSON yes, but it is not recommended because you may have some decoding problem. Your web framework is doing the automatic conversion for you. If you generate JSON manually, you can put the latter in this form.

  • In case, I take a string from the database, use json_decode, enter the information, and then use json_encode, and put it back in the database, and there in the database appears \u00aa, the problem may be in the database ??

  • Use json_decode before sending to database.

  • As it is in array, I use the json_encode to turn into string to be able to insert into the database. In case I use json_decode in an array it says json_decode() expects parameter 1 to be string, array given, I didn’t quite understand what you wanted me to do.

Show 2 more comments

Browser other questions tagged

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