怎么解決javascriptjson亂碼問題

這篇文章主要講解了“怎么解決javascript json亂碼問題”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么解決javascript json亂碼問題”吧!

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了恒山免費(fèi)建站歡迎大家使用!

javascript json亂碼的解決辦法:1、在json官網(wǎng)拷貝json.js文件;2、在HTML頁面引入json;3、通過“charset=GB2312”改變編碼方式為GB2312即可。

怎么解決javascript json亂碼問題

本文操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。

如何解決javascript json 亂碼問題?

javascript json的使用(以及中文亂碼的解決亂碼)

對于js使用json,首先到官網(wǎng)拷貝json.js文件,地址http://www.json.org/js.html

然后在頁面引入即可使用:

<script type="text/javascript" src="json.js"></script>

測試一下能不能用,可以寫個jsp頁面,然后給按鈕加個方法,彈出處理后的json字符串。

對于中文亂碼,改變編碼方式為:GB2312。

可以在頁面直接改,也可以在response返回時改。

頁面的改法:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>

java代碼response返回改法:

response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/json;charset=gb2312");

下面看看代碼吧,介紹了字符串與json對象相互轉(zhuǎn)化:(本例用到了jquery,只不過調(diào)用了一個按鈕單擊事件,你可以改成onclick)

<%@ page language="java"  pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>Insert title here</title>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
 
<style>
  button { margin:4px; cursor:pointer; }
  input { margin:4px; color:blue; }
  </style>
 
</head>
 
<body>
<div>
    <button id="button3">json</button>
  </div>
  <input type="text" value="click a button" />
<script>
/////////#button3
    
$("#button3").click(function () {
    ///json對象轉(zhuǎn)化成j字符串--toJSONString()
///字符串轉(zhuǎn)化成json對象--eval('(' + str + ')');
///取json對象里面某個屬性的值用點(diǎn)(例如myObject.bindings[0].time)
var myJSONObject = {"bindings": [
        {"nnn": "春天", "time": "標(biāo)示", "add": "北京"},
        {"ircEvent": "好", "method": "方法", "regex": "上海"}
    ] };
 
    alert("json對象取屬性值\n"+myJSONObject.bindings[0].nnn );
 
    ///json對象轉(zhuǎn)化成json字符串方法:
var myJSONtext=myJSONObject.toJSONString();
alert("json對象轉(zhuǎn)化成字符串,toJSONString\n"+myJSONtext);
var myJSONtext2=JSON.stringify(myJSONObject);
alert("json對象轉(zhuǎn)化成字符串,用全局的內(nèi)置對象JSON.stringify\n"+myJSONtext2);
 
    ///json字符串轉(zhuǎn)化成json對象方法:
var myJSONObject2 = eval('(' + myJSONtext + ')');
var myJSONObject3=myJSONtext.parseJSON();
var myJSONObject4=JSON.parse(myJSONtext);
 
alert("字符串轉(zhuǎn)化成json對象,用eval\n"+myJSONObject2.bindings[0].time );
alert("字符串轉(zhuǎn)化成json對象,用parseJSON\n"+myJSONObject3.bindings[0].add );
alert("字符串轉(zhuǎn)化成json對象,用全局的內(nèi)置對象JSON.parse\n"+myJSONObject4.bindings[0].nnn );
    /////////////////
    });
 
</script>
 
</body>
</html>

感謝各位的閱讀,以上就是“怎么解決javascript json亂碼問題”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么解決javascript json亂碼問題這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

新聞標(biāo)題:怎么解決javascriptjson亂碼問題
網(wǎng)站鏈接:http://bm7419.com/article4/pcdhoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣軟件開發(fā)、品牌網(wǎng)站制作、品牌網(wǎng)站設(shè)計、面包屑導(dǎo)航、App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名