Resolves an issue where JSON contains HTML tags that cannot be displayed

Source: Internet
Author: User



The main is to escape the JSON unrecognized characters


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;
}
The returned data will be displayed as expected, and the following is the converted content:
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 Conversion Functions
public string changestring (String str)
{
STR contains HTML-tagged text
str = str. Replace ("<", "&lt;");
str = str. Replace (">", "&gt;");
str = str. Replace ("", "&nbsp;");
str = str. Replace ("\ n", "<br>");
str = str. Replace ("&", "&amp;");
return str;
}
=======================



JS character filter HTML tag mutual transfer function
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;
}




Example 1



Rt:
The JSON data returned is as follows, with an HTML tag that cannot be displayed,



{"Articlelist": [{"ArticleID": 6, "title": "Goodbye Goodbye", "Author": "Superman", "Content": "BBBBBBBBBBBB", "hits": "0"}, {" ArticleID ": 7," "title": "333", "Author": "Superman", "Content": "333333333333333333333333333333333333", "hits": "0"}, {" ArticleID ": 1," "title": "Assad", "Author": "Heshan", "Content": "Test", "hits": "0"}, {"ArticleID": 2, "title": "Assad", "author ":" Superman "," Content ":" 123<br/> "," hits ":" 0 "},{" ArticleID ": 3," title ":" TEST1 "," Author ":" Superman "," Content ":" TEST1 <br/> "," hits ":" 0 "},{" ArticleID ": 4," title ":" Test "," Author ":" Superman "," Content ":" Test function!!!! "," hits ":" 0 "}, {" ArticleID ": 5," "title": "Test!", "Author": "Superman", "Content": "KE.util.setFullHtml (' content ', ');", "hits": "0"},]}



Workaround:
"Content": "123 <br/> "
Switch
"Content": "123<br/> "



js Method: Str.replaceall ("\" "," ' ");



Example 2:



After passing the data to the client in JSON format from the server side, some special characters cannot be displayed directly when the HTML page is displayed via JS, such as "<b>msg</b> #" displayed in the HTML page via JS when the background is passed. , not msg #, because the content between < and > is treated as an HTML tag, and the # with the & Start is HTML entity, so the display is not normal.



The solution is very simple, in JS to render it to the HTML page before the conversion can be:


01	<script type="text/javascript">
02	var str = ‘<b>msg</b> #‘;
03	document.all.div1.innerHTML=‘<pre>‘+str+‘</pre>‘;
04
05 / / the string in JS is normally displayed in HTML page
06	String.prototype.displayHtml= function(){
07 / / convert string to array
08	    var strArr = this.split(‘‘);
09 / / HTML page displays special characters. The nature of space is not, but when there are multiple spaces, the browser only displays one by default, so replace
10	    var htmlChar="&amp;<>";
11	    for(var i = 0; i< str.length;i++){
12 / / find if there are special HTML characters
13	        if(htmlChar.indexOf(str.charAt(i)) !=-1){
14 / / if they exist, convert them to corresponding HTML entities
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 ‘&amp;‘:
23	                    strArr.splice(i,1,‘&amp;‘);
24	            }
25}
26}
27	    return strArr.join(‘‘);
28}
29	alert(str.displayHtml());
30	document.all.div2.innerHTML=str.displayHtml();
31 </script>


Resolves an issue where JSON contains HTML tags that cannot be displayed


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.