最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

utf 8 - javascript chinesejapanese character decoding - Stack Overflow

programmeradmin5浏览0评论

I created a JSONP function on the server and returns a UTF-8 encoded json object like this

applyLocalization({"Name":"%E5%90%8D%E5%89%8D","Age":"%E5%B9%B4%E9%BD%A2"});

on my javascript on the client side, i want to convert the garbled part to their original state like

{"Name":"名前", "Age":"年齢"}

I tried $.parseJSON() but it doesnt work

I created a JSONP function on the server and returns a UTF-8 encoded json object like this

applyLocalization({"Name":"%E5%90%8D%E5%89%8D","Age":"%E5%B9%B4%E9%BD%A2"});

on my javascript on the client side, i want to convert the garbled part to their original state like

{"Name":"名前", "Age":"年齢"}

I tried $.parseJSON() but it doesnt work

Share Improve this question asked Jan 13, 2012 at 7:55 Chinchan ZuChinchan Zu 9185 gold badges20 silver badges40 bronze badges 6
  • How are you encoding the text on the server? There's a JSON standard for encoding UTF-8, which looks different... – deceze Commented Jan 13, 2012 at 8:03
  • what i do is i encode the key and the value by using URLEncoder.encode(str, "UTF-8") function in java. is there any otherway? – Chinchan Zu Commented Jan 13, 2012 at 8:39
  • What about using a JSON encoding function. I am not a Java guy, but a function that can take a native array and spit out the JSON equivalent exists for virtually every language... – deceze Commented Jan 13, 2012 at 8:40
  • actually this is what i do... i read a file written in japanese and what i do is i create a JSON object in java and put a key and the value (which is in japanese), but before i put it i encode it using the URLEncoder then the result is like this {"Name":"%E5%90%8D%E5%89%8D","Age":"%E5%B9%B4%E9%BD%A2"}.... then i send this to the client and thats where i plan to decode it using decodeURIComponent as what the fellas below said – Chinchan Zu Commented Jan 13, 2012 at 9:04
  • Why don't you skip the URL encoding? Then there's also no need to URL decode it. – deceze Commented Jan 13, 2012 at 9:07
 |  Show 1 more ment

2 Answers 2

Reset to default 9

You can use decodeURIComponent to decode urlencoded strings like yours

decodeURIComponent('%E5%90%8D%E5%89%8D');
//result: '名前'

You could use the decodeURIComponent function. But you shouldn't be URL encoding your javascript strings. You should send them as UTF-8 strings as-is. Javascript is capable of understanding them.

发布评论

评论列表(0)

  1. 暂无评论