2

I want to write a php json webservice that have persian string in mysql db. this is my table: enter image description here

this is my code: enter image description here

and result is: enter image description here

if you see result you will see persian strings show as numbers and back slashes. why? i try any code to debug it but can not do it. please help why persian strings not appear correctly.

PProg
  • 77
  • 10
  • It's basically no problem that the string is encoded this way in its plain form. JSON encodes all unusual utf characters into this hexadecimal representation. If you parse the JSON as a Javascript code or with any JSON parser, the string should be displayed correctly, try alert(theEncodedString) and you'll see if the encoding is actually corrupt or not. – amik Mar 30 '14 at 14:19
  • What version of PHP use? – Meysam Hit Mar 30 '14 at 14:24
  • 1
    Stackoverflow supports text. Please use text to represent your code instead of low resolution screenshots. – Quentin Mar 30 '14 at 14:38
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 30 '14 at 14:39
  • possible duplicate of [Reference: Why are my “special” Unicode characters encoded weird using json_encode?](http://stackoverflow.com/q/22745662/476) – deceze Mar 30 '14 at 15:13
  • hi friends i got it in Maks3w's answer. thanks for all your answers – PProg Mar 31 '14 at 11:32

3 Answers3

5

Just set the unescape option in json_encode

JSON_UNESCAPED_UNICODE (integer) Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0. http://php.net/manual/en/json.constants.php

json_encode(array('Cinema' => $cinemas), JSON_UNESCAPED_UNICODE );
Maks3w
  • 6,014
  • 6
  • 37
  • 42
1

if you see result you will see persian strings show as numbers and back slashes. why?

Because PHP's json_encode, by default, outputs ASCII JSON, so non-ASCII characters are represented by escape sequences.

why persian strings not appear correctly

There is nothing incorrect about using escape sequences for those characters.

You can use the JSON_UNESCAPED_UNICODE option if you want to use unicode literals instead of escape sequences.

json_encode(array('Cinema' => $cinemas), JSON_UNESCAPED_UNICODE );
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • @Maks3w — From the *question* (except, obviously, I had to transcribe it because the question is a picture for some reason). – Quentin Mar 30 '14 at 14:40
0

That is normal. JSON does support UTF-8 strings, however for maximum compatibility PHP encodes all non-ASCII characters as unicode escape sequences, \u1324 with 1234 being the character's unicode position in hexadecimal.

The receiving code, for instance JavaScript, will decode these sequences when the string is run through JSON.parse.

Ultimately, it does't really matter how it works so long as it works, and in this case your code works ;)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592