Parse error Rich Text JSON string (with double quotes) Quick Fix _c# Tutorial

Source: Internet
Author: User
Tags parse error

The company's project, passed back by JSON is this thing:

NewsId ":" 94f52614-8764-46d7-a5fe-d0da1fe878ed "," Newstitle ":" The large-scale public elective course "sustainable development and the future" series Two of modern economy (green economy) began to select online Class registration "," Newscontent ":" <span style= "font-size:12pt;font-family: Song body; color:black;line-height:150%;" ><span> recently, accompanied by our school Zheng academician, Professor Dong in Siping, Jiading held a wonderful speech, this semester, our school to build a large public elective course "sustainable development and the future" series one has been successfully concluded. This course is also one of the core compulsory courses of our university </span> "<span> Sustainable development specialty </span>" <span>. </span></span> 
<p style= "text-indent:21pt;" >
</p>
..... </span>

The root cause of unresolved queries is the double quotes "" and the backslash "\".

It is not possible to escape JSON directly, otherwise it will escape the double quotes from the JSON itself, so it cannot be escaped with brute force

How to find online:

Change the double quotation marks inside the bad JSON data to Chinese double quotes (whatever, as long as it's not double quotes) public
	String Jsonstringconvert (string s) {
    		char[] temp = S.tochararray ();    
    		int n = temp.length;
    		for (int i =0;i<n;i++) {
      	if (temp[i]== ': ' &&temp[i+1]== ' ') {for
          (int J =i+2;j<n;j++) {
            if ( temp[j]== ' "') {
              if (temp[j+1]!= ', ' && temp[j+1]!= '} ') {
                temp[j]= '";
              } else if (temp[j+1]== ', ' | | temp[j+1]== '} ') {break
                ;    
    }}}} return new String (temp);
  </span>

This method can be used to escape double quotes outside of the double quotes of the JSON itself into Chinese double quotes (anything else). This can be escaped as the correct JSON string.

Memo:

Before this method, the HTML code should also be used to space, otherwise JSON will not be able to parse, go to the space method:

public string Replaceblank (String str) {
		string dest = "";
	if (str!= null) {pattern
			p = pattern.compile ("\\s*|\t|\r|\n");
		Matcher m = p.matcher (str);
			Dest = M.replaceall ("");
			Pattern P2 = pattern.compile ("\\s*\");
			Matcher m2 = p2.matcher (dest);
			Dest = M2.replaceall ("\");
			Dest = Dest.replace ("=\" "," = ' ");
			p = pattern.compile ("\" \0*>);
			m = P.matcher (dest);
			Dest = M.replaceall (">");
		}
		return dest;
} </span>

The above parsing error Rich Text JSON string (with double quotes) Quick Solution is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.