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 ("<", "<");
str = str. Replace (">", ">");
str = str. Replace ("", " ");
str = str. Replace ("\ n", "<br>");
str = str. Replace ("&", "&");
return str;
}
=======================
JS character filter HTML tag mutual transfer function
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, ' & #39; ');
return str;
}
function HtmlDecode (str) {
str = str.replace (/&/gi, ' & ');
str = str.replace (/ /gi, ");
str = str.replace (/"/gi, ' "');
str = str.replace (/& #39;/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;
}
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>‘ ; |
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(); |
Resolves an issue where JSON contains HTML tags that cannot be displayed