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
2 Answers
Reset to default 9You 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.