2

For example

 {"orderNumber":"S301020000","customerFirstName":"ke ČECHA ","customerLastName":"张科","orderStatus":"PENDING_FULFILLMENT_REQUEST","orderSubmittedDate":"May 13, 2015 1:41:28 PM"}

how to get the accented character like "Č" in above json string and escape it in java

Just give some context of this question, please check this question from me Ajax unescape response text from java servlet not working properly

Sorry for my English :)

Community
  • 1
  • 1
ke zhang
  • 65
  • 1
  • 10

1 Answers1

2

You should escape all characters that are greater than 0x7F. You can loop through the String's characters using the .charAt(index) method. For each character ch that needs escaping, replace it with:

String hexDigits = Integer.toHexString(ch).toUpperCase();
String escapedCh = "\\u" + "0000".substring(hexDigits.length) + hexDigits;

I don't think you will need to unescape them in JavaScript because JavaScript supports escaped characters in string literals, so you should be able to work with the string the way it is returned by the server. I'm guessing you will be using JSON.parse() to convert the returned JSON string into a JavaScript object, like this.


Here's a complete function:

public static escapeJavaScript(String source)
{
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < 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();
}
John S
  • 21,212
  • 8
  • 46
  • 56
  • can you please explain "\\u" + zeroPad(Integer.toHexString(ch).toUpperCase(), 4) – ke zhang May 15 '15 at 03:54
  • @kezhang - That code will create a String like `\u5F20`. There should always be four digits, so the digits must be zero-padded on the left. – John S May 15 '15 at 04:00
  • ok Thanks, why do you think I don't need to unescape the unicode, I need to get the unescaped value like Č in javascript – ke zhang May 15 '15 at 04:02
  • 1
    @kezhang - JavaScript supports escaped characters in string literals, so you should be able to work with the string the way it is returned by the server. I'm guessing you will be using `JSON.parse()` to convert the returned JSON string into a JavaScript object, [like this](http://jsfiddle.net/qud3yhgw/). – John S May 15 '15 at 04:12
  • appreciate you help, I will try this overnight. I think I also need to figure out how the escape work, seems like Chinese does't treat as accented character, but why? – ke zhang May 15 '15 at 04:16
  • Sorry, I edit your answer, I put your explanation for jquery deal with Unicode in your answer, hope your don't mind, I should ask you first – ke zhang May 15 '15 at 04:19
  • @kezhang - You don't have high enough reputation to make an edit without out it being approved, but I think your edit was a good idea so I approved it. – John S May 15 '15 at 04:30
  • Just a comment to add that you can html-escape your strings with the same method,with a line like `String escapedCh = "" + "0000".substring(hexDigits.length()) + hexDigits + ";";` – Alexis Dufrenoy Oct 06 '21 at 12:22