如何将javascript unicode表示法转换为utf-8?

leo*_*eon 13 javascript unicode utf-8

有没有可以将javascript unicode符号代码转换为utf-8的命令行工具或在线服务?

例如,我得到了这个json代码,但很难在常见的文本编辑器中编辑.

{"name":"leon zhang","city":"\u4e0a\u6d77"}
Run Code Online (Sandbox Code Playgroud)

我想将其转换为:

{"name":"leon zhang","city":"??"}
Run Code Online (Sandbox Code Playgroud)

小智 13

您使用以下javascript函数将unicode转换为utf-8

function encode_utf8( s ){
    return unescape( encodeURIComponent( s ) );
}( '\u4e0a\u6d77' )
Run Code Online (Sandbox Code Playgroud)

您也可以在firebug控制台中试用它.有用.


wyp*_*wyp 2

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n    <html xmlns="http://www.w3.org/1999/xhtml">\n    <head>\n    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />\n    <title>UTF-8 To Unicode</title>\n    <style type="text/css">\n    <!--\n    td {\n        height: 24px;\n        line-height: 24px;\n    }\n    #content {\n        margin-top: 2px;\n    }\n    -->\n    </style>\n    </head>\n    <script type="text/javascript">\n        function encodeGB2312(){\n            var c = document.all.content.value;\n            var re = /(%)+/g;\n            var result =  escape(c);\n            document.all.content.value =  result.replace(re, "\\\\");\n            document.all.content.focus();\n            document.all.content.select();\n        }\n        function clearContent(){\n            document.all.content.value = "";\n            document.all.content.focus();\n        }\n    </script>\n\n    <body>\n        <h3 align="center">UTF-8 To Unicode</h3>\n        <table width="600" border="1" cellspacing="0" cellpadding="0" align="center"         style="text-align:center;">\n          <tr>\n            <td>Input what you want to convert\xef\xbc\x9a</td>\n            <td><input type="text" name="content" id="content" size="50"/></td>\n          </tr>\n          <tr>\n            <td colspan="2">\n            <input type="button" name="encoding" value="Start Conversion"         onclick="encodeGB2312()"/>&nbsp;&nbsp;&nbsp;&nbsp;\n            <input type="button" name="clear" value=" Cancel" onclick="clearContent();"/></td>\n          </tr>\n        </table>\n    </body>\n    </html>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是utf-8转unicode的示例,依次进行。

\n

  • 抱歉打扰,这是一个英语网站 - 请将您的解释翻译成该语言。 (2认同)