json包含單雙引號問題解決方案,json雙引號

來源:互聯網
上載者:User

json包含單雙引號問題解決方案,json雙引號

原創作品  轉載請註明出處


最近項目中  遇到需要JSON傳資料  但是某個欄位 裡面可能含有 雙引號和單引號  導致出現錯誤


為了方便 寫了一個工具類,比較好用。

支援“鏈式編程”


  分享給大家

360網盤:http://yunpan.cn/cjCrRZj9uc8hE  訪問密碼 79f3


效果DEMO:




JsonQuotesUtil.js

/** * 解決json傳輸資料時存在 同時存在單引號和雙引號的問題 * * 思路: * 將 單雙引號用不容易在字串中出現的字元分別替換 * 在後台 分別用過單雙引號替換掉即可 * 註:可以傳入字串 也可以傳入字串數組 * * author: 明明如月 QQ 605283073 * time:2015年5月19日15:33:44 */ function JsonQuotesUtil() { var defualtSingleQuotePlaceholder="s%0";//預設單引號預留位置 var defualtDoubleQuotePlaceholder="d%1";//預設雙引號預留位置 var singleQuotePlaceholder=defualtSingleQuotePlaceholder;//單引號預留位置 var doubleQuotePlaceholder=defualtDoubleQuotePlaceholder;//雙引號預留位置 //設定單引號預留位置(建議起不容易出現的字元) this.setSingleQuotePlaceholder = function(single) { singleQuotePlaceholder=single; return this; } //設定雙引號預留位置(建議起不容易出現的字元) this.setDoubleQuotePlaceholder = function(double) { doubleQuotePlaceholder=double; return this; } //恢複預設單引號和雙引號預留位置 this.restoreDefaults = function() {  singleQuotePlaceholder=defualtSingleQuotePlaceholder;  doubleQuotePlaceholder=defualtDoubleQuotePlaceholder; return this; } //用單引號預留位置替換單引號 並返回替換後的字串 this.replaceSingleQuote=function (str) { if(str instanceof  Array)//如果是數組分別替換 { for(var i=0;i<str.length;i++) {                str[i]= str[i].replace(/'/g, singleQuotePlaceholder); } }else { str= str[i].replace(/'/g, singleQuotePlaceholder); }  return str; }  //用雙引號替換符替換雙引號 並返回替換後的字串 this.replaceDoubleQuote = function (str) {// return str.replace(/"/g, doubleQuotePlaceholder); if(str instanceof  Array)//如果是數組分別替換 { 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)//如果是數組分別替換 { alert("1"); for(var i=0;i<str.length;i++) { alert(str[i]); str[i]= str[i].replace(/'/g,singleQuotePlaceholder).replace(/"/g, doubleQuotePlaceholder); } }else { str= str.replace(/'/g,singleQuotePlaceholder).replace(/"/g, doubleQuotePlaceholder); } return str; }   } 

demo.js 使用範例:

/** * Created by Administrator on 2015/5/19 0019. */$(function(){    //替換單雙引號按鈕的點擊事件    $("#show").click(function(){        var sourceStr =$("#sourceStr").val();//擷取需要替換字元        /* 使用方法1 建立對象並自訂 單雙引號預留位置           var jsonQuotesUtil = new JsonQuotesUtil().setSingleQuotePlaceholder("%^^").setDoubleQuotePlaceholder("&&");//建立對象(同時自訂單雙引預留位置)*/        /* 使用方法2 自訂對象並自訂 單雙引號預留位置           var jsonQuotesUtil = new JsonQuotesUtil();//建立對象使用預設單雙引號預留位置          jsonQuotesUtil.setSingleQuotePlaceholder("%^^");          jsonQuotesUtil.setDoubleQuotePlaceholder("##");*/         // jsonQuotesUtil.restoreDefaults();//恢複預設的單雙引號預留位置        /* 使用方法3 自訂對象並自訂 單雙引號預留位置         var jsonQuotesUtil = new JsonQuotesUtil();//建立對象使用預設單雙引號預留位置          */        var jsonQuotesUtil = new JsonQuotesUtil();//建立對象使用預設單雙引號預留位置        var single =$("#single").val();//擷取 單引號預留位置        var double = $("#double").val();//擷取輸入的雙引號預留位置        if($.trim(single)!="")        {            jsonQuotesUtil.setSingleQuotePlaceholder(single);//設定單引號預留位置        }        if($.trim(double)!="")        {            jsonQuotesUtil.setDoubleQuotePlaceholder(double);//設定雙引號預留位置        }        var reuslt = jsonQuotesUtil.replaceSingleAndDoubleQuote(sourceStr);//同時替換單雙引號         $("#replaceResult").html(reuslt);//顯示替換後的字串    });    function arrayTest()//支援數組每個字串的替換(沒有調用)    {        var jsonQuotesUtil = new JsonQuotesUtil();//建立對象使用預設單雙引號預留位置        var sourceStr = new Array();        sourceStr[0]="dfdfd'dfdf";        sourceStr[1]="dfdfd\"sfdsfsd";        alert("sourceStr"+sourceStr);        var reuslt = jsonQuotesUtil.replaceSingleAndDoubleQuote(sourceStr);//同時替換單雙引號        alert( "after:"+reuslt);    }});


JsonQuotesUtil Demo.html

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title>JsonQuotesUtil Demo</title>    <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></head>    請輸入帶有單雙引號的字串:<br/>  <textarea rows="10" cols="65" id="sourceStr">This is a simple solution for SingleQuote( ') and DoubleQuote(") in json </textarea><br/><br/>     單引號預留位置:<input id="single" type="text" placeholder="s%0"/>   雙引號預留位置:<input id="double" type="text" placeholder="d%1"/><br/> <br/>          <input type="button" id="show" value="替換單雙引號">         <br/>        <span id="replaceResult"></span></form></body></html>


後台解析Util:


package reg;/** * 解決json傳輸資料時存在 同時存在單引號和雙引號的問題 * * 思路: * 將 單雙引號用不容易在字串中出現的字元分別替換 * 在後台 分別用過單雙引號替換掉即可 * 註:可以傳入字串 也可以傳入字串數組 * * author: 明明如月 QQ 605283073 * time:2015年5月19日15:33:44 */public class JsonQuotesUtil {private  String defualtSingleQuotePlaceholder="s%0";//預設單引號預留位置private   String defualtDoubleQuotePlaceholder="d%1";//預設雙引號預留位置public JsonQuotesUtil() {super();}public JsonQuotesUtil(String defualtSingleQuotePlaceholder,String defualtDoubleQuotePlaceholder) {super();this.defualtSingleQuotePlaceholder = defualtSingleQuotePlaceholder;this.defualtDoubleQuotePlaceholder = defualtDoubleQuotePlaceholder;}//恢複單引號(字串)public    String restoreSingleQuotes(String str){return str.replaceAll(defualtSingleQuotePlaceholder, "\'");}public  String[]  restoreSingleQuotes(String[] strs)//恢複單引號(數組){for(int i =0;i<strs.length;i++){strs[i]= strs[i].replaceAll(defualtSingleQuotePlaceholder, "\'");}     return strs;}//恢複雙引號public   String restoreDoubleQuote(String str)//恢複單引號(數組){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;}//恢複單雙引號public   String restoreSingleAndDoubleQuote(String str){return str.replaceAll(defualtSingleQuotePlaceholder, "\'").replaceAll(defualtDoubleQuotePlaceholder, "\"");}public   String[] restoreSingleAndDoubleQuote(String[] strs)//恢複單雙引號(數組){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使用方法:


package reg;public class JsonQuotesUtilDemo {public static void main(String args[]){//擷取前台傳入的參數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));}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.