Solution to the single double quotation mark in json format

Source: Internet
Author: User

Solution to the single double quotation mark in json format

 

Source of original work reprinted

 

In recent projects, JSON data transmission is required, but a field may contain double quotation marks and single quotation marks, leading to errors.

 

 

A tool class is easy to use.

Support for "chain programming"

 

 

Effect DEMO:

 

 

JsonQuotesUtil. js

 

/*** Solve the problem of single quotation marks and double quotation marks when json data is transmitted: * replace single double quotation marks with characters that are not easy to appear in strings * replace single double quotation marks with single ones in the background respectively * Note: you can input a string or an array of strings ** author: Mingyue QQ 605283073 * time: May 19, 2015 15:33:44 */function JsonQuotesUtil () {var defualtSingleQuotePlaceholder = s % 0; // default single quotes placeholder var defualtDoubleQuotePlaceholder = d % 1; // default double quotes placeholder var singleQuotePlaceholder = defualtSingleQuotePlaceholder; // single quotes placeholder var doubleQuotePlaceholder = accept; // double quotation mark placeholder // set the single quotation mark placeholder (it is recommended that the characters are not easy to appear) this. setSingleQuotePlaceholder = function (single) {singleQuotePlaceholder = single; return this;} // set the double quotation mark placeholder (it is recommended that you start the characters that are not easy to appear) this. setDoubleQuotePlaceholder = function (double) {doubleQuotePlaceholder = double; return this;} // restores the default single quotes and double quotes placeholder this. restoreDefaults = function () {singleQuotePlaceholder = placeholder; doubleQuotePlaceholder = defualtDoubleQuotePlaceholder; return this;} // replace single quotes with single quotes placeholders and return the replaced string this. replaceSingleQuote = function (str) {if (str instanceof Array) // replace {for (var I = 0; I
 
  
Demo. js example:
  

 

/*** Created by Administrator on 0019. */$ (function () {// click event to replace the single double quotation mark button $ (# show ). click (function () {var sourceStr =$ (# sourceStr ). val (); // get the character to be replaced/* use method 1 to create an object and customize a single double quotation mark placeholder var jsonQuotesUtil = new JsonQuotesUtil (). setSingleQuotePlaceholder (% ^ ). setDoubleQuotePlaceholder (&); // create an object (custom single-and double-cited placeholders at the same time) * // * use method 2 to customize an object and a single double quotation mark placeholder var jsonQuotesUtil = new JsonQuotesUtil (); // create an object using the default single double quotation mark placeholder jsonQuot EsUtil. setSingleQuotePlaceholder (% ^); jsonQuotesUtil. setDoubleQuotePlaceholder (#); * // jsonQuotesUtil. restoreDefaults (); // restore the default single double quotation mark placeholder/* use method 3 custom object and custom single double quotation mark placeholder var jsonQuotesUtil = new JsonQuotesUtil (); // create an object using the default single double quotation mark placeholder */var jsonQuotesUtil = new JsonQuotesUtil (); // create an object using the default single double quotation mark placeholder var single =$ (# single ). val (); // obtain the single quotation mark placeholder var double =$ (# double ). val (); // obtain the input double quotation mark placeholder if ($. trim (single )! =) {JsonQuotesUtil. setSingleQuotePlaceholder (single); // set single quotes placeholder} if ($. trim (double )! =) {JsonQuotesUtil. setDoubleQuotePlaceholder (double); // set the double quotation mark placeholder} var reuslt = jsonQuotesUtil. replaceSingleAndDoubleQuote (sourceStr); // replace a single double quotation mark simultaneously (character replaceresult)).html (reuslt); // display the replaced string}); function arrayTest () // supports replacement of each string in the Array (not called) {var jsonQuotesUtil = new JsonQuotesUtil (); // creates an object using the default single-quotes placeholder var sourceStr = new Array (); sourceStr [0] = dfdfd 'dfdf; sourceStr [1] = dfdfdsfdsfsd; alert (sourceStr + sourceStr); var reuslt = jsonQuotesUtil. replaceSingleAndDoubleQuote (sourceStr); // Replace the single double quotation mark alert (after: + reuslt );}});

 

JsonQuotesUtil Demo.html

 


  
  <Script type = text/javascript src = libs/js/jquery. js> </script> <script type = text/javascript src = libs/js/JsonQuotesUtil. js> </script> <script type = text/javascript src = libs/js/demo. js> </script> enter a string with single double quotation marks:
  
This is a simple solution for SingleQuote (') and DoubleQuote () in json

Single quotes placeholder: Double quotation mark placeholder:


 

 

Background parsing Util:

 

 

Package reg;/*** solve the problem of single quotation marks and double quotation marks when json data is transmitted simultaneously: * replace single double quotation marks with characters that are not easy to appear in strings * replace single double quotation marks with single ones in the background respectively * Note: you can input a String or an array of strings ** author: Mingyue QQ 605283073 * time: May 19, 2015 15:33:44 */public class JsonQuotesUtil {private String defualtSingleQuotePlaceholder = s % 0; // default single quotes placeholder private String defualtDoubleQuotePlaceholder = d % 1; // default double quotes placeholder public JsonQuotesUtil () {super ();} public JsonQuotesUtil (String defualtSingleQuotePlaceholder, String Region) {super (); this. defualtSingleQuotePlaceholder = defualtSingleQuotePlaceholder; this. defualtDoubleQuotePlaceholder = defualtDoubleQuotePlaceholder;} // restore single quotes (String) public String restoreSingleQuotes (String str) {return str. replaceAll (defualtSingleQuotePlaceholder, ');} public String [] restoreSingleQuotes (String [] strs) // restore single quotes (arrays) {for (int I = 0; I
  
   
Util usage:
   

 

 

 

Package reg; public class JsonQuotesUtilDemo {public static void main (String args []) {// obtain the input parameter String str = This is a simple solution for SingleQuote (s % 0) and DoubleQuote (d % 1) in json This is a simple solution for SingleQuote (s % 0) and DoubleQuote (d % 1) in json; jsonQuotesUtil jsonQuotesUtil = new JsonQuotesUtil (s % 0, d % 1); System. out. println (jsonQuotesUtil. restoreSingleAndDoubleQuote (str ));}}


 

 

Related Article

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.