2012-05-14 87 views
10

有沒有可以將javascript unicode符號代碼轉換爲utf-8的命令行工具或在線服務?如何將javascript unicode符號代碼轉換爲utf-8?

E.g.我得到了這個json代碼,但在普通的文本編輯器中很難編輯。

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

我想將其轉換爲:

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

可能的重複[如何使用* nix中的控制檯工具將\ uXXXX unicode轉換爲UTF-8](http://stackoverflow.com/questions/8795702/how-to-convert-uxxxx-unicode-to-utf -8-using-console-tools-in-nix) –

+0

我找到了一個在線工具,http://www.rishida.net/tools/conversion/。它可以將unicode符號代碼轉換爲很多格式。 另一種方式是將該內容發佈到Chrome開發者控制檯,broswer會將其轉換並顯示在utf-8中。 – leon

回答

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> 

這是轉換的UTF-8爲Unicode的樣本,這樣做反過來。

+1

對不起,但這是一個英文網站 - 請將您的解釋翻譯成該語言。 –

+0

:-)好的,我認爲@leon是中國人,所以... – wyp

7

您可以使用JavaScript函數以下爲Unicode轉換爲UTF-8

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

你也可以嘗試一下在Firebug控制檯。有用。

相關問題