1. JSON strings in Java contain HTML tags
/**
* JSON contains HTML tags for text
* @param str
* @return
*/
public static string Switchstr (String str) {
str = str.replace ("<", "<");
str = str.replace (">", ">");
str = str.replace ("", " ");
str = str.replace ("&", "&");
str = str.replace ("\ n", "<br>");
return str;
}
For example, the JSON string contains HTML tags
String json= "<a href= ' http://www.baidu.com ' > Baidu </a>";
System.out.println (test.switchstr (URL));
Output result: &lt;a&nbsp;href= ' http://www.baidu.com ' &gt; Baidu &lt;/a&gt;
2. The HTML tags in the string in JS are converted to each other
(1) Convert HTML tags to json
function HTMLEncode (str) {
str = str.replace (/&/g, ' & ');
str = str.replace (/</g, ' < ');
str = str.replace (/>/g, ' > ');
str = str.replace (//g, ' ‘);
str = str.replace (/t/g, ' ');
str = str.replace (/x22/g, ' " ');
str = str.replace (/(?: t| |v|r) *n/g, ' <br/> ');
str = str.replace (/x27/g, ' & #39; ');
return str;
}
Example: Parsing a tag in JS into a string
var url = "<a href= ' http://www.baidu.com ' > Baidu </a>";
Alert (HTMLEncode (URL));
Results: &l&<br/>bsp;&<br/>bsp;; A href= ' h&<br/>bsp;&<br/>bsp;&<br/>bsp;&<br/>bsp;p://www.baidu.com ' &G&<BR/>bsp;&<br/>bsp;; Baidu &l&<br/>bsp;&<br/>bsp;;/ A&G&<BR/>bsp;&<br/>bsp;;
(2) Convert JSON to HTML tags
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;
}
For example: The string in the JSON in JS is converted to HTML tags
var json = "&lt;a&nbsp;href= ' http://www.baidu.com ' &gt; Baidu &lt;/a&gt;";
Alert (HtmlDecode (JSON));
Results: <a href= ' http://www.baidu.com ' > Baidu </a>
JSON and HTML tags convert to each other