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":"
//r//n//t
//r//n"}]}';
HTML转换函数
public string ChangeString(string str)
{
//str含有HTML标签的文本
str = str.Replace("<","<");
str = str.Replace(">",">");
str = str.Replace(" "," ");
str = str.Replace("\n","
");
str = str.Replace("&","&");
return str;
}
=======================
js字符过滤html标签互转函数
function htmlencode(str) {
str = str.replace(/&/g, '&');
str = str.replace(/ str = str.replace(/>/g, '>');
str = str.replace(/(?:t| |v|r)*n/g, '
');
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(/
]*>(?:(rn)|r|n)?/gi, 'n');
return str;
}
function textencode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/ str = str.replace(/>/g, '>');
return str;
}
function textdecode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/</gi, '<');
str = str.replace(/>/gi, '>');
return str;
}
实例1
{"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
","hits":"0"},{"articleId":3,"title":"TEST1","author":"superman","content":"TEST1
","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
"
改为
"content":"123
"
js方法:str.replaceAll("\"","'");
实例2:
从服务器端以JSON格式将数据传递到客户端后,通过JS显示在HTML页面时,有一些特殊字符不能直接显示,如后台传递过来的是 'msg #' 通过JS显示在HTML页面中时,显示成了 msg # ,并不是msg #,这是由于<与>之间的内容看作是HTML标签了,而以&开头的 与#为HTML实体,所以显示不正常。
解决办法很简单,在JS将其渲染到HTML页面前转换一下即可:
01 |
|