如何将javascript unicode符号代码转换为utf-8?

时间:2023-01-05 16:21:01

Is there a command line tool or online service that can convert javascript unicode notation code to utf-8?

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

E.g. I got this json code, but it is hard to edit in common text editor.

我有这个json代码,但是在普通的文本编辑器中很难编辑。

{"name":"leon zhang","city":"\u4e0a\u6d77"}

I want to convert it to:

我想把它转换成:

{"name":"leon zhang","city":"上海"}

2 个解决方案

#1


8  

You use the below javascript function to convert unicode to utf-8

使用下面的javascript函数将unicode转换为utf-8。

function encode_utf8( s ){
    return unescape( encodeURIComponent( s ) );
}( '\u4e0a\u6d77' )

You can also try it out in firebug console. It works.

您也可以在firebug控制台试用它。它的工作原理。

#2


2  

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>UTF-8 To Unicode</title>
    <style type="text/css">
    <!--
    td {
        height: 24px;
        line-height: 24px;
    }
    #content {
        margin-top: 2px;
    }
    -->
    </style>
    </head>
    <script type="text/javascript">
        function encodeGB2312(){
            var c = document.all.content.value;
            var re = /(%)+/g;
            var result =  escape(c);
            document.all.content.value =  result.replace(re, "\\");
            document.all.content.focus();
            document.all.content.select();
        }
        function clearContent(){
            document.all.content.value = "";
            document.all.content.focus();
        }
    </script>

    <body>
        <h3 align="center">UTF-8 To Unicode</h3>
        <table width="600" border="1" cellspacing="0" cellpadding="0" align="center"         style="text-align:center;">
          <tr>
            <td>Input what you want to convert:</td>
            <td><input type="text" name="content" id="content" size="50"/></td>
          </tr>
          <tr>
            <td colspan="2">
            <input type="button" name="encoding" value="Start Conversion"         onclick="encodeGB2312()"/>&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" name="clear" value=" Cancel" onclick="clearContent();"/></td>
          </tr>
        </table>
    </body>
    </html>

This is a sample of convert utf-8 to unicode, do it in turn.

这是将utf-8转换为unicode的示例,请依次执行。

#1


8  

You use the below javascript function to convert unicode to utf-8

使用下面的javascript函数将unicode转换为utf-8。

function encode_utf8( s ){
    return unescape( encodeURIComponent( s ) );
}( '\u4e0a\u6d77' )

You can also try it out in firebug console. It works.

您也可以在firebug控制台试用它。它的工作原理。

#2


2  

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>UTF-8 To Unicode</title>
    <style type="text/css">
    <!--
    td {
        height: 24px;
        line-height: 24px;
    }
    #content {
        margin-top: 2px;
    }
    -->
    </style>
    </head>
    <script type="text/javascript">
        function encodeGB2312(){
            var c = document.all.content.value;
            var re = /(%)+/g;
            var result =  escape(c);
            document.all.content.value =  result.replace(re, "\\");
            document.all.content.focus();
            document.all.content.select();
        }
        function clearContent(){
            document.all.content.value = "";
            document.all.content.focus();
        }
    </script>

    <body>
        <h3 align="center">UTF-8 To Unicode</h3>
        <table width="600" border="1" cellspacing="0" cellpadding="0" align="center"         style="text-align:center;">
          <tr>
            <td>Input what you want to convert:</td>
            <td><input type="text" name="content" id="content" size="50"/></td>
          </tr>
          <tr>
            <td colspan="2">
            <input type="button" name="encoding" value="Start Conversion"         onclick="encodeGB2312()"/>&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" name="clear" value=" Cancel" onclick="clearContent();"/></td>
          </tr>
        </table>
    </body>
    </html>

This is a sample of convert utf-8 to unicode, do it in turn.

这是将utf-8转换为unicode的示例,请依次执行。