PDA

View Full Version : Json to handle names with special character?



shaoen01
2 Feb 2012, 7:35 AM
Hi All,

I am using a grid to pull out data from mysql database. I realized that names with special characters such as "Gaņanita", this field will not be displayed in my grid. I suspect it is due to JSON not able to handle it when i sent over to my grid?

I am using Firebug to debug and i see null values being passed. And when i check in my database table, the name is there. Any ideas on how to tackle this? Thanks

mitchellsimoens
2 Feb 2012, 9:28 AM
Easy way to see if it is the JSON is to plug your JSON into:

http://jsonlint.com/

skirtle
2 Feb 2012, 2:55 PM
Your description of seeing nulls makes me suspicious that the problem lies elsewhere but here's some general advice on working with non-ASCII characters.

Try to use UTF-8 to encode the characters to bytes, UTF-8 is the default encoding for JSON:

http://www.ietf.org/rfc/rfc4627.txt?number=4627

Alternatively you could use \uXXXX escaping on such characters, though that may be tricky if you're using a library to do the JSON creation for you:

http://json.org/

If you're using such characters in the dataIndex for a grid column then it may be the JS file that is being misinterpreted rather than the JSON. Try using \uXXXX escaping in your JS file. You can also use script tag attributes and response headers to control the character encoding.

shaoen01
4 Feb 2012, 4:48 AM
Thanks for the response. It seems that maybe it might not be Json. I find that the output in my php from the database is already null when i use json_encode(). Maybe i need to escape the characters before i use json_encode()? What do you think?

shaoen01
4 Feb 2012, 4:18 PM
I managed to solve the issue with this code below in my PHP file. Thanks skirtle (http://www.sencha.com/forum/member.php?197255-skirtle) and all!


mysql_query('SET CHARACTER SET utf8');

soemi
7 Aug 2013, 9:15 AM
Thank you all... I use the code and all special characters show up :D

mysql_query('SET CHARACTER SET utf8');