解决JSON包含HTML标签无法显示的问题

主要是将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("<","&lt;");
str = str.Replace(">","&gt;");
str = str.Replace(" ","&nbsp;");
str = str.Replace("\n","<br>");
str = str.Replace("&","&amp;");
return str;
}
=======================

js字符过滤html标签互转函数
function htmlencode(str) {
 str = str.replace(/&/g, '&amp;');
 str = str.replace(/</g, '&lt;');
 str = str.replace(/>/g, '&gt;');
 str = str.replace(/(?:t| |v|r)*n/g, '<br />');
 str = str.replace(/  /g, '&nbsp; ');
 str = str.replace(/t/g, '&nbsp; &nbsp; ');
 str = str.replace(/x22/g, '&quot;');
 str = str.replace(/x27/g, '&#39;');
 return str;
}

function htmldecode(str) {
 str = str.replace(/&amp;/gi, '&');
 str = str.replace(/&nbsp;/gi, ' ');
 str = str.replace(/&quot;/gi, '"');
 str = str.replace(/&#39;/g, "'");
 str = str.replace(/&lt;/gi, '<');
 str = str.replace(/&gt;/gi, '>');
 str = str.replace(/<br[^>]*>(?:(rn)|r|n)?/gi, 'n');
 return str;
}

function textencode(str) {
 str = str.replace(/&amp;/gi, '&');
 str = str.replace(/</g, '&lt;');
 str = str.replace(/>/g, '&gt;');
 return str;
}

function textdecode(str) {
 str = str.replace(/&amp;/gi, '&');
 str = str.replace(/&lt;/gi, '<');
 str = str.replace(/&gt;/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>';  
04    
05 //js中的字符串正常显示在HTML页面中  
06 String.prototype.displayHtml= function(){  
07     //将字符串转换成数组  
08     var strArr = this.split('');  
09     //HTML页面特殊字符显示,空格本质不是,但多个空格时浏览器默认只显示一个,所以替换  
10     var htmlChar="&<>";  
11     for(var i = 0; i< str.length;i++){  
12         //查找是否含有特殊的HTML字符  
13         if(htmlChar.indexOf(str.charAt(i)) !=-1){  
14             //如果存在,则将它们转换成对应的HTML实体  
15             switch (str.charAt(i)) {                          
16                 case '<':  
17                     strArr.splice(i,1,'<');  
18                     break;  
19                 case '>':  
20                     strArr.splice(i,1,'>');  
21                     break;  
22                 case '&':  
23                     strArr.splice(i,1,'&');  
24             }  
25         }  
26     }  
27     return strArr.join('');  
28 }  
29 alert(str.displayHtml());  
30 document.all.div2.innerHTML=str.displayHtml();  

 

31 </script> 
 
 
posted on 2017-09-12 11:46  雨季的雾  阅读(688)  评论(0编辑  收藏  举报