比尔云BierYun--阿里云最新优惠活动
阿里云优惠码丨阿里云代金券

json包含单双引号问题解决方案

json包含单双引号问题解决方案http://www.bieryun.com/1197.html

最近项目中  遇到需要JSON传数据  但是某个字段 里面可能含有 双引号和单引号  导致出现错误

为了方便 写了一个工具类,比较好用。

支持“链式编程”

分享给大家

360网盘下载地址:http://yunpan.cn/cjCrRZj9uc8hE  访问密码 79f3

效果DEMO:

JsonQuotesUtil.js

[javascript] view plain copy

  1. /**
  2.  * 解决json传输数据时存在 同时存在单引号和双引号的问题
  3.  *
  4.  * 思路:
  5.  *
  6.  * 1 首选将 双引号转义
  7.  *
  8.  * 2 将 单双引号用不容易在字符串中出现的字符分别替换
  9.  *   在后台 分别用过单双引号替换掉即可
  10.  *
  11.  * 注:可以传入字符串 也可以传入字符串数组
  12.  * author: 明明如月 QQ 605283073
  13.  * time:2015年5月19日15:33:44
  14.  */
  15.  function JsonQuotesUtil()
  16.  {
  17.      var defualtSingleQuotePlaceholder=“s%0”;//默认单引号占位符
  18.      var defualtDoubleQuotePlaceholder=“d%1”;//默认双引号占位符
  19.      var singleQuotePlaceholder=defualtSingleQuotePlaceholder;//单引号占位符
  20.      var doubleQuotePlaceholder=defualtDoubleQuotePlaceholder;//双引号占位符
  21.     //设置单引号占位符(建议起不容易出现的字符)
  22.      this.setSingleQuotePlaceholder = function(single)
  23.      {
  24.          singleQuotePlaceholder=single;
  25.          return this;
  26.      }
  27.     //设置双引号占位符(建议起不容易出现的字符)
  28.      this.setDoubleQuotePlaceholder = function(double)
  29.      {
  30.          doubleQuotePlaceholder=double;
  31.          return this;
  32.      }
  33.     //恢复默认单引号和双引号占位符
  34.      this.restoreDefaults = function()
  35.      {
  36.          singleQuotePlaceholder=defualtSingleQuotePlaceholder;
  37.          doubleQuotePlaceholder=defualtDoubleQuotePlaceholder;
  38.         return this;
  39.      }
  40.     //用单引号占位符替换单引号 并返回替换后的字符串
  41.      this.replaceSingleQuote=function (str)
  42.      {
  43.          if(str instanceof  Array)//如果是数组分别替换
  44.          {
  45.              for(var i=0;i<str.length;i++)
  46.              {
  47.                 str[i]= str[i].replace(/’/g, singleQuotePlaceholder);
  48.              }
  49.          }else
  50.          {
  51.              str= str[i].replace(/’/g, singleQuotePlaceholder);
  52.          }
  53.           return str;
  54.      }
  55.      //用双引号替换符替换双引号 并返回替换后的字符串
  56.      this.replaceDoubleQuote = function (str)
  57.      {
  58.         // return str.replace(/”/g, doubleQuotePlaceholder);
  59.          if(str instanceof  Array)//如果是数组分别替换
  60.          {
  61.              for(var i=0;i<str.length;i++)
  62.              {
  63.                  str[i]= str[i].replace(/’/g, doubleQuotePlaceholder);
  64.              }
  65.          }else
  66.          {
  67.              str= str[i].replace(/’/g, doubleQuotePlaceholder);
  68.          }
  69.          return str;
  70.      }
  71.      this.replaceSingleAndDoubleQuote = function(str)
  72.      {
  73.         // return str.replace(/’/g,singleQuotePlaceholder).replace(/”/g, doubleQuotePlaceholder);
  74.          if(str instanceof  Array)//如果是数组分别替换
  75.          {
  76.              alert(“1”);
  77.              for(var i=0;i<str.length;i++)
  78.              {
  79.                  alert(str[i]);
  80.                  str[i]= str[i].replace(/’/g,singleQuotePlaceholder).replace(/”/g, doubleQuotePlaceholder);
  81.              }
  82.          }else
  83.          {
  84.              str= str.replace(/’/g,singleQuotePlaceholder).replace(/”/g, doubleQuotePlaceholder);
  85.          }
  86.          return str;
  87.      }
  88.      //双引号转义
  89.      this.escapeDoubleQuote = function(str)
  90.      {
  91.          if(str instanceof  Array)//如果是数组分别替换
  92.          {
  93.              alert(“1”);
  94.              for(var i=0;i<str.length;i++)
  95.              {
  96.                  alert(str[i]);
  97.                  str[i]= str[i].replace(/“/g,”\\\“”);
  98.              }
  99.          }else
  100.          {
  101.              str= str.replace(/“/g,”\\\“”);;
  102.          }
  103.          return str;
  104.      }
  105.  }

 

demo.js 使用范例:

[javascript] view plain copy

  1. /**
  2.  * Created by Administrator on 2015/5/19 0019.
  3.  */
  4. $(function(){
  5.     //替换单双引号按钮的点击事件
  6.     $(“#show”).click(function(){
  7.         var sourceStr =$(“#sourceStr”).val();//获取需要替换字符
  8.         /* 使用方法1 创建对象并自定义 单双引号占位符
  9.            var jsonQuotesUtil = new JsonQuotesUtil().setSingleQuotePlaceholder
  10. (“%^^”).setDoubleQuotePlaceholder(“&&”);//创建对象(同时自定义单双引占位符)*/
  11.         /* 使用方法2 自定义对象并自定义 单双引号占位符
  12.            var jsonQuotesUtil = new JsonQuotesUtil();//创建对象使用默认单双引号占位符
  13.           jsonQuotesUtil.setSingleQuotePlaceholder(“%^^”);
  14.           jsonQuotesUtil.setDoubleQuotePlaceholder(“##”);*/
  15.          // jsonQuotesUtil.restoreDefaults();//恢复默认的单双引号占位符
  16.         /* 使用方法3 自定义对象并自定义 单双引号占位符
  17.          var jsonQuotesUtil = new JsonQuotesUtil();//创建对象使用默认单双引号占位符
  18.           */
  19.         var jsonQuotesUtil = new JsonQuotesUtil();//创建对象使用默认单双引号占位符
  20.         var single =$(“#single”).val();//获取 单引号占位符
  21.         var double = $(“#double”).val();//获取输入的双引号占位符
  22.         if($.trim(single)!=“”)
  23.         {
  24.             jsonQuotesUtil.setSingleQuotePlaceholder(single);//设置单引号占位符
  25.         }
  26.         if($.trim(double)!=“”)
  27.         {
  28.             jsonQuotesUtil.setDoubleQuotePlaceholder(double);//设置双引号占位符
  29.         }
  30.         var reuslt = jsonQuotesUtil.replaceSingleAndDoubleQuote(sourceStr);//同时替换单双引
  31.        // var reuslt = jsonQuotesUtil.escapeDoubleQuote(sourceStr)
  32.          $(“#replaceResult”).html(reuslt);//显示替换后的字符串
  33.     });
  34.     $(“#escape”).click(function(){
  35.         var sourceStr =$(“#sourceStr”).val();//获取需要替换字符
  36.         var reuslt =  new JsonQuotesUtil().escapeDoubleQuote(sourceStr);
  37.         $(“#replaceResult”).html(reuslt);//显示替换后的字符串
  38.     });
  39.     function arrayTest()//支持数组每个字符串的替换(没有调用)
  40.     {
  41.         var jsonQuotesUtil = new JsonQuotesUtil();//创建对象使用默认单双引号占位符
  42.         var sourceStr = new Array();
  43.         sourceStr[0]=“dfdfd’dfdf”;
  44.         sourceStr[1]=“dfdfd\”sfdsfsd”;
  45.         alert(“sourceStr”+sourceStr);
  46.         var reuslt = jsonQuotesUtil.replaceSingleAndDoubleQuote(sourceStr);//同时替换单双引
  47.         alert( “after:”+reuslt);
  48.     }
  49. });

JsonQuotesUtil Demo.html

[html] view plain copy

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang=“en”>
  4.     <meta charset=“UTF-8”>
  5.     <title>JsonQuotesUtil Demo</title>
  6.     <script type=“text/javascript” src=“libs/js/jquery.js”></script>
  7.     <script type=“text/javascript” src=“libs/js/JsonQuotesUtil.js”></script>
  8.     <script type=“text/javascript” src=“libs/js/demo.js”></script>
  9. </head>
  10.     请输入带有单双引号的字符串:<br/>
  11.           <textarea rows=“10” cols=“65” id=“sourceStr”>This is a simple solution for SingleQuote( ‘) and DoubleQuote(“) in json </textarea>
  12. <br/><br/>
  13.      单引号占位符:<input id=“single” type=“text” placeholder=“s%0”/> &nbsp; 双引号占位符:<input id=“double” type=“text” placeholder=“d%1”/>
  14. <br/> <br/>
  15.           <input type=“button” id=“show” value=“替换单双引号”>&nbsp;<input type=“button” id=“escape” value=“转义双引号”>
  16.          <br/>
  17.         <span id=“replaceResult”></span>
  18. </form>
  19. </body>
  20. </html>

后台解析Util:

[java] view plain copy

  1. package reg;
  2. /**
  3.  * 解决json传输数据时存在 同时存在单引号和双引号的问题
  4.  *
  5.  * 思路:
  6.  * 将 单双引号用不容易在字符串中出现的字符分别替换
  7.  * 在后台 分别用过单双引号替换掉即可
  8.  * 注:可以传入字符串 也可以传入字符串数组
  9.  *
  10.  * author: 明明如月 QQ 605283073
  11.  * time:2015年5月19日15:33:44
  12.  */
  13. public class JsonQuotesUtil
  14. {
  15.     private  String defualtSingleQuotePlaceholder=“s%0”;//默认单引号占位符
  16.     private   String defualtDoubleQuotePlaceholder=“d%1”;//默认双引号占位符
  17.     public JsonQuotesUtil()
  18.     {
  19.         super();
  20.     }
  21.     public JsonQuotesUtil(String defualtSingleQuotePlaceholder,
  22.             String defualtDoubleQuotePlaceholder)
  23.     {
  24.         super();
  25.         this.defualtSingleQuotePlaceholder = defualtSingleQuotePlaceholder;
  26.         this.defualtDoubleQuotePlaceholder = defualtDoubleQuotePlaceholder;
  27.     }
  28.     //恢复单引号(字符串)
  29.     public    String restoreSingleQuotes(String str)
  30.     {
  31.         return str.replaceAll(defualtSingleQuotePlaceholder, “\'”);
  32.     }
  33.     public  String[]  restoreSingleQuotes(String[] strs)//恢复单引号(数组)
  34.     {
  35.         for(int i =0;i<strs.length;i++)
  36.         {
  37.             strs[i]= strs[i].replaceAll(defualtSingleQuotePlaceholder, “\'”);
  38.         }
  39.          return strs;
  40.     }
  41.     //恢复双引号
  42.     public   String restoreDoubleQuote(String str)//恢复单引号(数组)
  43.     {
  44.         return str.replaceAll(defualtDoubleQuotePlaceholder, “\””);
  45.     }
  46.     public   String[] restoreDoubleQuote(String[] strs)
  47.     {
  48.         for(int i =0;i<strs.length;i++)
  49.         {
  50.             strs[i]= strs[i].replaceAll(defualtSingleQuotePlaceholder, “\'”);
  51.         }
  52.          return strs;
  53.     }
  54.     //恢复单双引号
  55.     public   String restoreSingleAndDoubleQuote(String str)
  56.     {
  57.         return str.replaceAll(defualtSingleQuotePlaceholder, “\'”).replaceAll(defualtDoubleQuotePlaceholder, “\””);
  58.     }
  59.     public   String[] restoreSingleAndDoubleQuote(String[] strs)//恢复单双引号(数组)
  60.     {
  61.         for(int i =0;i<strs.length;i++)
  62.         {
  63.             strs[i]= strs[i].replaceAll(defualtSingleQuotePlaceholder, “\'”).replaceAll(defualtDoubleQuotePlaceholder, “\””);
  64.         }
  65.          return strs;
  66.     }
  67.     public String getDefualtSingleQuotePlaceholder()
  68.     {
  69.         return defualtSingleQuotePlaceholder;
  70.     }
  71.     public void setDefualtSingleQuotePlaceholder(String defualtSingleQuotePlaceholder)
  72.     {
  73.         this.defualtSingleQuotePlaceholder = defualtSingleQuotePlaceholder;
  74.     }
  75.     public String getDefualtDoubleQuotePlaceholder()
  76.     {
  77.         return defualtDoubleQuotePlaceholder;
  78.     }
  79.     public void setDefualtDoubleQuotePlaceholder(String defualtDoubleQuotePlaceholder)
  80.     {
  81.         this.defualtDoubleQuotePlaceholder = defualtDoubleQuotePlaceholder;
  82.     }
  83. }

Util使用方法:

[java] view plain copy

  1. package reg;
  2. public class JsonQuotesUtilDemo
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         //获取前台传入的参数
  7.         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”;
  8.         JsonQuotesUtil jsonQuotesUtil = new JsonQuotesUtil(“s%0”,“d%1”);
  9.         System.out.println(jsonQuotesUtil.restoreSingleAndDoubleQuote(str));
  10.     }
  11. }

 

未经允许不得转载:比尔云 » json包含单双引号问题解决方案
分享到: 更多 (0)

相关推荐

  • 暂无文章

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

强烈推荐

高性能SSD云服务器ECS抗攻击,高可用云数据库RDS