Flash 如何在AS3中解码utf8字符串?

Flash 如何在AS3中解码utf8字符串?,flash,actionscript-3,utf-8,Flash,Actionscript 3,Utf 8,我的AS3 Flash加载了一些JSON文件,其中包含一些特殊字符: quelques caract\\u00e8res sp\\u00e9ciaux(更新) 在使用了一些str\u replace('\\\','\\',myVar)之后,我确实去掉了双斜杠,但它仍然不会显示为重音 有没有办法在AS3中解码这个字符串(getbacké,èchars…) 谢谢 我将unescape函数从代码中提取到一个helper类中 public class StringHelper { /**

我的AS3 Flash加载了一些JSON文件,其中包含一些特殊字符:
quelques caract\\u00e8res sp\\u00e9ciaux
(更新)

在使用了一些
str\u replace('\\\','\\',myVar)
之后,我确实去掉了双斜杠,但它仍然不会显示为重音

有没有办法在AS3中解码这个字符串(getbacké,èchars…)


谢谢

我将unescape函数从代码中提取到一个helper类中

public class StringHelper
{
    /**
     * Convert all JavaScript escape characters into normal characters
     *
     * @param input The input string to convert
     * @return Original string with escape characters replaced by real characters
     */
    public static function unescapeString( input:String ):String
    {
        var result:String = "";
        var backslashIndex:int = 0;
        var nextSubstringStartPosition:int = 0;
        var len:int = input.length;

        do
        {
            // Find the next backslash in the input
            backslashIndex = input.indexOf( '\\', nextSubstringStartPosition );

            if ( backslashIndex >= 0 )
            {
                result += input.substr( nextSubstringStartPosition, backslashIndex - nextSubstringStartPosition );

                // Move past the backslash and next character (all escape sequences are
                // two characters, except for \u, which will advance this further)
                nextSubstringStartPosition = backslashIndex + 2;

                // Check the next character so we know what to escape
                var escapedChar:String = input.charAt( backslashIndex + 1 );
                switch ( escapedChar )
                {
                    // Try to list the most common expected cases first to improve performance

                    case '"':
                        result += escapedChar;
                        break; // quotation mark
                    case '\\':
                        result += escapedChar;
                        break; // reverse solidus   
                    case 'n':
                        result += '\n';
                        break; // newline
                    case 'r':
                        result += '\r';
                        break; // carriage return
                    case 't':
                        result += '\t';
                        break; // horizontal tab    

                    // Convert a unicode escape sequence to it's character value
                    case 'u':

                        // Save the characters as a string we'll convert to an int
                        var hexValue:String = "";

                        var unicodeEndPosition:int = nextSubstringStartPosition + 4;

                        // Make sure there are enough characters in the string leftover
                        if ( unicodeEndPosition > len )
                        {
                            parseError( "Unexpected end of input.  Expecting 4 hex digits after \\u." );
                        }

                        // Try to find 4 hex characters
                        for ( var i:int = nextSubstringStartPosition; i < unicodeEndPosition; i++ )
                        {
                            // get the next character and determine
                            // if it's a valid hex digit or not
                            var possibleHexChar:String = input.charAt( i );
                            if ( !isHexDigit( possibleHexChar ) )
                            {
                                parseError( "Excepted a hex digit, but found: " + possibleHexChar );
                            }

                            // Valid hex digit, add it to the value
                            hexValue += possibleHexChar;
                        }

                        // Convert hexValue to an integer, and use that
                        // integer value to create a character to add
                        // to our string.
                        result += String.fromCharCode( parseInt( hexValue, 16 ) );

                        // Move past the 4 hex digits that we just read
                        nextSubstringStartPosition = unicodeEndPosition;
                        break;

                    case 'f':
                        result += '\f';
                        break; // form feed
                    case '/':
                        result += '/';
                        break; // solidus
                    case 'b':
                        result += '\b';
                        break; // bell
                    default:
                        result += '\\' + escapedChar; // Couldn't unescape the sequence, so just pass it through
                }
            }
            else
            {
                // No more backslashes to replace, append the rest of the string
                result += input.substr( nextSubstringStartPosition );
                break;
            }

        } while ( nextSubstringStartPosition < len );

        return result;
    }

    /**
     * Determines if a character is a digit [0-9].
     *
     * @return True if the character passed in is a digit
     */
    private static function isDigit( ch:String ):Boolean
    {
        return ( ch >= '0' && ch <= '9' );
    }

    /**
     * Determines if a character is a hex digit [0-9A-Fa-f].
     *
     * @return True if the character passed in is a hex digit
     */
    private static function isHexDigit( ch:String ):Boolean
    {
        return ( isDigit( ch ) || ( ch >= 'A' && ch <= 'F' ) || ( ch >= 'a' && ch <= 'f' ) );
    }

    /**
     * Raises a parsing error with a specified message, tacking
     * on the error location and the original string.
     *
     * @param message The message indicating why the error occurred
     */
    private static function parseError( message:String ):void
    {
        throw new Error( message );
    }

}

我将unescape函数从代码中提取到一个helper类中

public class StringHelper
{
    /**
     * Convert all JavaScript escape characters into normal characters
     *
     * @param input The input string to convert
     * @return Original string with escape characters replaced by real characters
     */
    public static function unescapeString( input:String ):String
    {
        var result:String = "";
        var backslashIndex:int = 0;
        var nextSubstringStartPosition:int = 0;
        var len:int = input.length;

        do
        {
            // Find the next backslash in the input
            backslashIndex = input.indexOf( '\\', nextSubstringStartPosition );

            if ( backslashIndex >= 0 )
            {
                result += input.substr( nextSubstringStartPosition, backslashIndex - nextSubstringStartPosition );

                // Move past the backslash and next character (all escape sequences are
                // two characters, except for \u, which will advance this further)
                nextSubstringStartPosition = backslashIndex + 2;

                // Check the next character so we know what to escape
                var escapedChar:String = input.charAt( backslashIndex + 1 );
                switch ( escapedChar )
                {
                    // Try to list the most common expected cases first to improve performance

                    case '"':
                        result += escapedChar;
                        break; // quotation mark
                    case '\\':
                        result += escapedChar;
                        break; // reverse solidus   
                    case 'n':
                        result += '\n';
                        break; // newline
                    case 'r':
                        result += '\r';
                        break; // carriage return
                    case 't':
                        result += '\t';
                        break; // horizontal tab    

                    // Convert a unicode escape sequence to it's character value
                    case 'u':

                        // Save the characters as a string we'll convert to an int
                        var hexValue:String = "";

                        var unicodeEndPosition:int = nextSubstringStartPosition + 4;

                        // Make sure there are enough characters in the string leftover
                        if ( unicodeEndPosition > len )
                        {
                            parseError( "Unexpected end of input.  Expecting 4 hex digits after \\u." );
                        }

                        // Try to find 4 hex characters
                        for ( var i:int = nextSubstringStartPosition; i < unicodeEndPosition; i++ )
                        {
                            // get the next character and determine
                            // if it's a valid hex digit or not
                            var possibleHexChar:String = input.charAt( i );
                            if ( !isHexDigit( possibleHexChar ) )
                            {
                                parseError( "Excepted a hex digit, but found: " + possibleHexChar );
                            }

                            // Valid hex digit, add it to the value
                            hexValue += possibleHexChar;
                        }

                        // Convert hexValue to an integer, and use that
                        // integer value to create a character to add
                        // to our string.
                        result += String.fromCharCode( parseInt( hexValue, 16 ) );

                        // Move past the 4 hex digits that we just read
                        nextSubstringStartPosition = unicodeEndPosition;
                        break;

                    case 'f':
                        result += '\f';
                        break; // form feed
                    case '/':
                        result += '/';
                        break; // solidus
                    case 'b':
                        result += '\b';
                        break; // bell
                    default:
                        result += '\\' + escapedChar; // Couldn't unescape the sequence, so just pass it through
                }
            }
            else
            {
                // No more backslashes to replace, append the rest of the string
                result += input.substr( nextSubstringStartPosition );
                break;
            }

        } while ( nextSubstringStartPosition < len );

        return result;
    }

    /**
     * Determines if a character is a digit [0-9].
     *
     * @return True if the character passed in is a digit
     */
    private static function isDigit( ch:String ):Boolean
    {
        return ( ch >= '0' && ch <= '9' );
    }

    /**
     * Determines if a character is a hex digit [0-9A-Fa-f].
     *
     * @return True if the character passed in is a hex digit
     */
    private static function isHexDigit( ch:String ):Boolean
    {
        return ( isDigit( ch ) || ( ch >= 'A' && ch <= 'F' ) || ( ch >= 'a' && ch <= 'f' ) );
    }

    /**
     * Raises a parsing error with a specified message, tacking
     * on the error location and the original string.
     *
     * @param message The message indicating why the error occurred
     */
    private static function parseError( message:String ):void
    {
        throw new Error( message );
    }

}
trace('quelques-caract\u00c3\u00a8res-sp\u00c3\u00a9ciaux')显示您需要的内容,因此它在默认情况下工作
upd:

private-var-escapedDict:Object={'n':'\n','r':'\r','t':'\t','f':'\f','b':'\b','':'\',''':''''\':''''\','''''':'\'''''''''\'.':'};
私有函数unDoubleEscape(src:String):String{
var arr:Array=src.split('\\');
对于(变量i:int=1;i
跟踪('quelques-caract\u00c3\u00a8res-sp\u00c3\u00a9ciaux')显示您需要的内容,因此它在默认情况下工作
upd:

private-var-escapedDict:Object={'n':'\n','r':'\r','t':'\t','f':'\f','b':'\b','':'\',''':''''\':''''\','''''':'\'''''''''\'.':'};
私有函数unDoubleEscape(src:String):String{
var arr:Array=src.split('\\');
对于(变量i:int=1;i
没有,我使用的是myTextField.htmlText=…&我在屏幕上看到“test-caract\\u00e8re”。@olouv-尝试
myTextField.text=…
myTextField.htmlText=String(…)
跟踪(…)
返回什么?我需要htmlText特性,所以我不能使用.text;跟踪返回“test caract\\u00e8re”;字符串不能解决这个问题。这两条斜线正常吗?似乎我应该使用一些带斜线或类似的东西,不是吗?@olouv-你肯定需要单斜线(如你的问题和我的回答中所示)我做了一些
str\u替换(“\\\\”,“\\\”,…)
,我确实在跟踪中得到了
donn\u00e9es
。。。(单破折号)。。。但仍然没有特别的角色!如果我跟踪('\u00e9'),我会得到!没有,我使用的是myTextField.htmlText=…&我在屏幕上看到“test-caract\\u00e8re”。@olouv-尝试
myTextField.text=…
myTextField.htmlText=String(…)
跟踪(…)
返回什么?我需要htmlText特性,所以我不能使用.text;跟踪返回“test caract\\u00e8re”;字符串不能解决这个问题。这两条斜线正常吗?似乎我应该使用一些带斜线或类似的东西,不是吗?@olouv-你肯定需要单斜线(如你的问题和我的回答中所示)我做了一些
str\u替换(“\\\\”,“\\\”,…)
,我确实在跟踪中得到了
donn\u00e9es
。。。(单破折号)。。。但仍然没有特别的角色!如果我跟踪('\u00e9'),我会得到!
private var escapedDict:Object = { 'n' : '\n', 'r' : '\r', 't' : '\t', 'f' : '\f', 'b' : '\b', '"' : '\"', "'" : '\'', '\\' : ''};
private function unDoubleEscape(src:String):String {
    var arr:Array = src.split('\\');
    for (var i:int = 1; i < arr.length; i++) {
        if (arr[i].charAt(0) != 'u') {
            arr[i] = escapedDict[arr[i].charAt(0)] ? escapedDict[arr[i].charAt(0)] + arr[i].substr(1) : arr[i];
        }else {
            arr[i] = String.fromCharCode('0x' + arr[i].slice(1, 5)) + arr[i].substr(5);
        }               
    }
    return arr.join('');
}