主要是將json無法識別的字符進行轉義
function dotran($str) {
$str = str_replace('"','//"',$str);
$str = str_replace("/r/n",'//r//n',$str);
$str = str_replace("/t",'//t',$str);
$str = str_replace("//",'//',$str);
$str = str_replace("/b",'//b',$str);
return $str;
}
這樣返回的數據就可以正常顯示,下面是轉換后的內容:
jsontext='{"jqry":[{"id":"121","userid":"0","status":"1","filename":"","url":"333333333","title":"aaaaaaa","type":"WatchTV","seq":"1","remark":"remarktext","content":"<p>//r//n//t<object classid=//"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000//" codebase=//"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0//"><param name=//"quality//" value=//"high//" /><param name=//"movie//" value=//"http://www.xxx.com/vod/COD4_Ep1.swf//" /><embed pluginspage=//"http://www.macromedia.com/go/getflashplayer//" quality=//"high//" src=//"http://www.xxx.com/vod/COD4_Ep1.swf//" type=//"application/x-shockwave-flash//"></embed></object></p>//r//n"}]}';
HTML轉換函數
public string ChangeString(string str)
{
//str含有HTML標簽的文本
str = str.Replace("<","<");
str = str.Replace(">",">");
str = str.Replace(" "," ");
str = str.Replace("\n","<br>");
str = str.Replace("&","&");
return str;
}
=======================
js字符過濾html標簽互轉函數
function htmlencode(str) {
str = str.replace(/&/g, '&');
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
str = str.replace(/(?:t| |v|r)*n/g, '<br />');
str = str.replace(/ /g, ' ');
str = str.replace(/t/g, ' ');
str = str.replace(/x22/g, '"');
str = str.replace(/x27/g, ''');
return str;
}
function htmldecode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/ /gi, ' ');
str = str.replace(/"/gi, '"');
str = str.replace(/'/g, "'");
str = str.replace(/</gi, '<');
str = str.replace(/>/gi, '>');
str = str.replace(/<br[^>]*>(?:(rn)|r|n)?/gi, 'n');
return str;
}
function textencode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
return str;
}
function textdecode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/</gi, '<');
str = str.replace(/>/gi, '>');
return str;
}
實例1
RT:
返回的Json數據如下、帶Html標簽就無法顯示了、
{"articleList": [{"articleId":6,"title":"拜拜拜拜 吧","author":"superman","content":"BBBBBBBBBBBB","hits":"0"}, {"articleId":7,"title":"333","author":"superman","content":"333333333333333333333333333333333333","hits":"0"}, {"articleId":1,"title":"阿薩 德","author":"heshan","content":"test","hits":"0"}, {"articleId":2,"title":"阿薩德","author":"superman","content":"123<img src="http://localhost:8080/jpkc/admin/kindeditor/plugins/emoticons/0.gif" alt="" border="0" /><br />","hits":"0"},{"articleId":3,"title":"TEST1","author":"superman","content":"TEST1<br />","hits":"0"},{"articleId":4,"title":"測 試","author":"superman","content":"測試功能!!!!","hits":"0"}, {"articleId":5,"title":"test!","author":"superman","content":"KE.util.setFullHtml('content', '');","hits":"0"},]}
解決辦法:
"content":"123<img src="http://localhost:8080/jpkc/admin/kindeditor/plugins/emoticons/0.gif" alt="" border="0"/><br/>"
改為
"content":"123<img src='http://localhost:8080/jpkc/admin/kindeditor/plugins/emoticons/0.gif' alt='' border='0' /><br/>"
js方法:str.replaceAll("\"","'");
實例2:
從服務器端以JSON格式將數據傳遞到客戶端后,通過JS顯示在HTML頁面時,有一些特殊字符不能直接顯示,如后台傳遞過來的是 '<b>msg</b> #' 通過JS顯示在HTML頁面中時,顯示成了 msg # ,並不是msg #,這是由於<與>之間的內容看作是HTML標簽了,而以&開頭的 與#為HTML實體,所以顯示不正常。
解決辦法很簡單,在JS將其渲染到HTML頁面前轉換一下即可:
01 |
<script type="text/javascript"> |
02 |
var str = '<b>msg</b> #'; |
03 |
document.all.div1.innerHTML='<pre>'+str+'</pre>'; |
06 |
String.prototype.displayHtml= function(){ |
08 |
var strArr = this.split(''); |
09 |
//HTML頁面特殊字符顯示,空格本質不是,但多個空格時瀏覽器默認只顯示一個,所以替換 |
11 |
for(var i = 0; i< str.length;i++){ |
13 |
if(htmlChar.indexOf(str.charAt(i)) !=-1){ |
14 |
//如果存在,則將它們轉換成對應的HTML實體 |
15 |
switch (str.charAt(i)) { |
17 |
strArr.splice(i,1,'<'); |
20 |
strArr.splice(i,1,'>'); |
23 |
strArr.splice(i,1,'&'); |
27 |
return strArr.join(''); |
29 |
alert(str.displayHtml()); |
30 |
document.all.div2.innerHTML=str.displayHtml(); |