0

I have received a json from server like below

{
    "AuthenticationMessage": "\\u0986\\u09aa\\u09a8\\u09bf \\u09ad\\u09c1\\u09b2 \\u09aa\\u09be\\u09b8\\u0993\\u09df\\u09be\\u09b0\\u09cd\\u09a1 \\u09a6\\u09bf\\u09df\\u09c7\\u099b\\u09c7\\u09a8"
}

and i also have a Textview like below

ValidationMessage = (TextView) findViewById(R.id.tv_validation_message);

now i want to print the value of "AuthenticationMessage" in the textview. i have done following

ValidationMessage.setText(response.getString("AuthenticationMessage"));

but it shows text like below

\u0986\u09aa\u09a8\u09bf \u09ad\u09c1\u09b2 \u09aa\u09be\u09b8\u0993\u09df\u09be\u09b0\u09cd\u09a1 \u09a6\u09bf\u09df\u09c7\u099b\u09c7\u09a8

i am looking for

আপনি ভুল পাসওয়ার্ড দিয়েছেন

what i am looking wrong. please guide me.

2
  • check this link: stackoverflow.com/questions/6017596/… Commented Nov 3, 2015 at 13:19
  • If the server would send a correct JSON unicode encoding, i.e. "\u0986\u09aa\u09a8 ..." everything would work out of the box. Its the wrong strategy to try to fix this on the client side. Commented Nov 3, 2015 at 13:28

2 Answers 2

6

I just copy other's codes.

Try to search what's unicode and how to convert char to unicode.

private static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Can you post a small explaination on your code? This would help others understand how you solve it.
You could just use Integer.parseInt(theString.subString(x, x+4), 16) to parse that hexadecimal number. I also think this will crash on a truncated sequence like "\u1"
1

Try this code. I didn't test it but should work

String myString = response.getString("AuthenticationMessage")
String[] strList = myString.split(" ");
String text = "";
for (String str : strList) {
    str = str.replace("\\\\","");
    String[] arr = str.split("u");
    for(int i = 0; i < arr.length; i++){
        int hexVal = Integer.parseInt(arr[i], 16);
        text += (char)hexVal;
    }
    text += " "
}

ValidationMessage.setText(text)

2 Comments

Getting Exception like below "java.lang.StringIndexOutOfBoundsException: length=1; regionStart=0; regionLength=3 at java.lang.String.startEndAndLength(String.java:588) at java.lang.String.substring(String.java:1475)"
because int i should start from 0. Edited the source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.