JSON and HTML tags convert to each other

Source: Internet
Author: User

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 ("<", "&lt;");
str = str.replace (">", "&gt;");
str = str.replace ("", "&nbsp;");
str = str.replace ("&", "&amp;");
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: &amp;lt;a&amp;nbsp;href= ' http://www.baidu.com ' &amp;gt; Baidu &amp;lt;/a&amp;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, ' &amp; ');
str = str.replace (/</g, ' &lt; ');
str = str.replace (/>/g, ' &gt; ');
str = str.replace (//g, ' &nbsp; ‘);
str = str.replace (/t/g, ' &nbsp;&nbsp; ');
str = str.replace (/x22/g, ' &quot; ');
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 ' &AMP;G&AMP;&LT;BR/>bsp;&<br/>bsp;; Baidu &l&<br/>bsp;&<br/>bsp;;/ A&AMP;G&AMP;&LT;BR/>bsp;&<br/>bsp;;

(2) Convert JSON to HTML tags
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;
}

For example: The string in the JSON in JS is converted to HTML tags
var json = "&amp;lt;a&amp;nbsp;href= ' http://www.baidu.com ' &amp;gt; Baidu &amp;lt;/a&amp;gt;";
Alert (HtmlDecode (JSON));
Results: <a href= ' http://www.baidu.com ' > Baidu </a>

JSON and HTML tags convert to each other

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.