JSON contains single double quotation mark problem solution

Source: Internet
Author: User

Original works reproduced please indicate the source


A recent project encountered a need for JSON data but a field may contain double quotes and single quotation marks resulting in an error


For the convenience of writing a tool class, more useful.

Support for "chained programming"


Share to everyone

360 Network disk: Http://yunpan.cn/cjCrRZj9uc8hE access password 79f3


Effect Demo:




Jsonquotesutil.js

/** * Solve the problem that there are both single and double quotes when JSON transmits data * idea: * Replace single and double quotes with characters that are not easy to appear in the string * in the background with a single double quotation mark replacement * Note: You can pass in a string or an array of strings * Autho R: Clearly such as the month QQ 605283073 * time:2015 May 19 15:33:44 * * Function Jsonquotesutil () {var defualtsinglequoteplaceholder= "s%0";//Default Single-quote placeholder var defualtdoublequoteplaceholder= "d%1";//default double-quote placeholder Var singlequoteplaceholder= defualtsinglequoteplaceholder;//Single-quote placeholder var doublequoteplaceholder=defualtdoublequoteplaceholder;//double-quote placeholder// Set single quotation mark placeholders (characters that are not likely to appear) This.setsinglequoteplaceholder = function (simple) {singlequoteplaceholder=single; return this;} Set double quotation mark placeholders (characters that are not likely to appear) This.setdoublequoteplaceholder = function (double) {doublequoteplaceholder=double; return This  }//restore default single quotes and double quote placeholders this.restoredefaults = function () {singlequoteplaceholder=defualtsinglequoteplaceholder; Doublequoteplaceholder=defualtdoublequoteplaceholder; return this; }//Replace the single quotation mark with a single quote placeholder and return the replaced string this.replacesinglequote=function (str) {if (str instanceof array)//If the array is replaced by {for (Var i=0;i&    lt;str.length;i++) {            str[i]= str[i].replace (/'/g, singlequoteplaceholder);  }}else {str= str[i].replace (/'/g, singlequoteplaceholder);} return str; }//Replace double quotation marks with double quote substitution and return the replaced string this.replacedoublequote = function (str) {//Return str.replace (/"/g, Doublequoteplaceholder ); if (str instanceof array)//If the array is replaced by {for (Var i=0;i<str.length;i++) {str[i]= str[i].replace (/'/g, Doublequoteplaceholder); }}else {str= str[i].replace (/'/g, doublequoteplaceholder);} return str; } this.replacesingleanddoublequote = function (str) {//Return str.replace (/'/g,singlequoteplaceholder). Replace (/"/g, Doublequoteplaceholder); if (str instanceof array)//If the array is replaced by {alert ("1") respectively, for (Var i=0;i<str.length;i++) {alert (str[i]); str[i]= Str[i].repla CE (/'/g,singlequoteplaceholder). Replace (/"/g, doublequoteplaceholder); }}else {str= str.replace (/'/g,singlequoteplaceholder). Replace (/"/g, doublequoteplaceholder);} return str;  }   }

Demo.js Use Example:

/** * Created by Administrator on 2015/5/19 0019.        */$ (function () {///Replace single double quote button for click event $ ("#show"). Click (function () {var sourcestr =$ ("#sourceStr"). Val ();//Gets the replacement character required /* Use Method 1 to create the object and customize the single-double-quote placeholder var jsonquotesutil = new Jsonquotesutil (). Setsinglequoteplaceholder ("% ^^"). setd  Oublequoteplaceholder ("&&");//Create object (with custom single-double-placeholder) *////* Use Method 2 to customize the object and customize the single double quote placeholder var jsonquotesutil =          New Jsonquotesutil ();//create object using default single double quote placeholder Jsonquotesutil.setsinglequoteplaceholder ("% ^^"); Jsonquotesutil.setdoublequoteplaceholder ("# #"); *///Jsonquotesutil.restoredefaults ();//Restore the default single-double-quote placeholder/* Consumer Method 3 Customizing the object and customizing the single-double-quote placeholder var jsonquotesutil = new Jsonquotesutil ();//create object using default single double quote placeholder */var jsonquotes Util = new Jsonquotesutil ();//Create an object using the default single double quote placeholder var a =$ ("#single"). Val ();//Gets the single-quote placeholder var double = $ ("#do Uble "). Val ();//Gets the double-quote placeholder for the input if ($.trim (single)! =" ") {Jsonquotesutil.setsinglequoteplaceHolder (single);//Set the apostrophe placeholder} if ($.trim (double)! = "") {Jsonquotesutil.setdoublequoteplacehol         Der (double);//Set double quote placeholder} var reuslt = Jsonquotesutil.replacesingleanddoublequote (SOURCESTR);//replace single double quote    $ ("#replaceResult"). HTML (REUSLT);//Display the replaced string}); function arraytest ()//support array for each string substitution (no call) {var jsonquotesutil = new Jsonquotesutil ();//create object using default single double quote placeholder VA        R sourcestr = new Array ();        sourcestr[0]= "DFDFD ' dfdf";        sourcestr[1]= "dfdfd\" SFDSFSD ";        Alert ("Sourcestr" +sourcestr);    var reuslt = Jsonquotesutil.replacesingleanddoublequote (SOURCESTR);//replace single and double quotes alert ("After:" +reuslt); }});


Jsonquotesutil demo.html

<! DOCTYPE html>


Background parsing util:


package reg;/** * Resolves an issue in which there are both single and double quotes when JSON transmits data * idea: * Replace single and double quotes with characters that are not easy to appear in the string * replace each other with single double quotes in the background * Note: You can pass in a string or you can pass in a string Array * * Author: Clearly like the month QQ 605283073 * time:2015 year May 19 15:33:44 */public class Jsonquotesutil {private String defualtsinglequo Teplaceholder= "s%0";//default single-quote placeholder private String defualtdoublequoteplaceholder= "d%1";//default double-quote placeholder public Jsonquotesutil ( ) {super ();} Public Jsonquotesutil (String defualtsinglequoteplaceholder,string defualtdoublequoteplaceholder) {super (); This.defualtsinglequoteplaceholder = Defualtsinglequoteplaceholder;this.defualtdoublequoteplaceholder = Defualtdoublequoteplaceholder;} Recover single quotation marks (strings) public string restoresinglequotes (String str) {return Str.replaceall (defualtsinglequoteplaceholder, "\ '") );} Public string[] Restoresinglequotes (string[] strs)//restore single quotation mark (array) {for (int i =0;i<strs.length;i++) {strs[i]= strs[i].     ReplaceAll (Defualtsinglequoteplaceholder, "\");} return STRs;} Recover double quotation mark public string Restoredoublequote (string str)//Recover single quotation mark (array) {return str.replaceaLL (Defualtdoublequoteplaceholder, "\" ");} Public string[] Restoredoublequote (string[] STRs) {for (int i =0;i<strs.length;i++) {strs[i]= Strs[i].replaceall (     Defualtsinglequoteplaceholder, "\");} return STRs;} Restore single double quotation mark public string Restoresingleanddoublequote (String str) {return Str.replaceall (Defualtsinglequoteplaceholder, "\"). ReplaceAll (Defualtdoublequoteplaceholder, "\" ");} Public string[] Restoresingleanddoublequote (string[] strs)//restore single double quotation mark (array) {for (int i =0;i<strs.length;i++) {strs[i]=     Strs[i].replaceall (Defualtsinglequoteplaceholder, "\"). ReplaceAll (Defualtdoublequoteplaceholder, "\" ");} return STRs;} Public String Getdefualtsinglequoteplaceholder () {return defualtsinglequoteplaceholder;} public void Setdefualtsinglequoteplaceholder (String defualtsinglequoteplaceholder) { This.defualtsinglequoteplaceholder = Defualtsinglequoteplaceholder;} Public String Getdefualtdoublequoteplaceholder () {return defualtdoublequoteplaceholder;} public void Setdefualtdoublequoteplaceholder (StrinG defualtdoublequoteplaceholder) {this.defualtdoublequoteplaceholder = Defualtdoublequoteplaceholder;}} 

Util How to use:


Package Reg;public class Jsonquotesutildemo {public static void main (string args[]) {//Get foreground incoming parameter string str= "This is a Simpl E Solution for Singlequote (s%0) and Doublequote (d%1) in JSON the is a easy solution for Singlequote (s%0) and Doublequot E (d%1) in JSON ";    Jsonquotesutil jsonquotesutil = new Jsonquotesutil ("s%0", "d%1"); SYSTEM.OUT.PRINTLN (Jsonquotesutil.restoresingleanddoublequote (str));}}



JSON contains single double quotation mark problem solution

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.