jquery - java how to escape accented character in string -
for example
{"ordernumber":"s301020000","customerfirstname":"ke Čecha ","customerlastname":"张科","orderstatus":"pending_fulfillment_request","ordersubmitteddate":"may 13, 2015 1:41:28 pm"}
how accented character "Č" in above json string , escape in java
just give context of question, please check question me ajax unescape response text java servlet not working properly
sorry english :)
you should escape characters greater 0x7f
. can loop through string's characters using .charat(index)
method. each character ch
needs escaping, replace with:
string hexdigits = integer.tohexstring(ch).touppercase(); string escapedch = "\\u" + "0000".substring(hexdigits.length) + hexdigits;
i don't think need unescape them in javascript because javascript supports escaped characters in string literals, should able work string way returned server. i'm guessing using json.parse() convert returned json string javascript object, like this.
here's complete function:
public static escapejavascript(string source) { stringbuilder result = new stringbuilder(); (int = 0; < source.length(); i++) { char ch = source.charat(i); if (ch > 0x7f) { string hexdigits = integer.tohexstring(ch).touppercase(); string escapedch = "\\u" + "0000".substring(hexdigits.length) + hexdigits; result.append(escapedch); } else { result.append(ch); } } return result.tostring(); }
Comments
Post a Comment