如何在Android中将UniCode字符串转换为Text

时间:2015-11-03 13:01:41

标签: java android json unicode

我收到了来自下面的服务器的json

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

我也有如下文字视图

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

现在我要打印" AuthenticationMessage"的值。在textview中。 我做了以下

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

但它显示如下文字

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

我正在寻找

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

我看错了。请指导我。

2 个答案:

答案 0 :(得分:5)

我只是复制其他代码。

尝试搜索什么是unicode以及如何将char转换为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();
    }

答案 1 :(得分:1)

试试这段代码。我没有测试它但应该工作

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)