解决方案:在后台处理 JSONArray.fromObject(list).toString()
转自明明如月小角落:
效果DEMO:
JsonQuotesUtil.js
-
- 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= str.replace(/'/g, singleQuotePlaceholder);
-
- }
- }else
- {
- str= str.replace(/'/g, singleQuotePlaceholder);
- }
- return str;
- }
-
-
- this.replaceDoubleQuote = function (str)
- {
-
-
- if(str instanceof Array)
- {
- for(var i=0;i<str.length;i++)
- {
- str= str.replace(/'/g, doubleQuotePlaceholder);
-
- }
- }else
- {
- str= str.replace(/'/g, doubleQuotePlaceholder);
- }
- return str;
-
- }
-
-
-
-
- this.replaceSingleAndDoubleQuote = function(str)
- {
-
- if(str instanceof Array)
- {
- alert("1");
- for(var i=0;i<str.length;i++)
- {
- alert(str);
- str= str.replace(/'/g,singleQuotePlaceholder).replace(/"/g, doubleQuotePlaceholder);
-
- }
- }else
- {
- str= str.replace(/'/g,singleQuotePlaceholder).replace(/"/g, doubleQuotePlaceholder);
- }
- return str;
- }
-
-
-
- this.escapeDoubleQuote = function(str)
- {
-
-
- if(str instanceof Array)
- {
- alert("1");
- for(var i=0;i<str.length;i++)
- {
- alert(str);
- str= str.replace(/"/g,"\\\"");
-
- }
- }else
- {
- str= str.replace(/"/g,"\\\"");;
- }
- return str;
- }
- }
-
demo.js 使用范例:
- $(function(){
-
-
- $("#show").click(function(){
-
- var sourceStr =$("#sourceStr").val();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
-
-
-
- });
-
-
-
- $("#escape").click(function(){
-
- var sourceStr =$("#sourceStr").val();
-
- var reuslt = new JsonQuotesUtil().escapeDoubleQuote(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="替换单双引号"> <input type="button" id="escape" value="转义双引号">
- <br/>
- <span id="replaceResult"></span>
-
-
-
- </form>
-
- </body>
- </html>
后台解析Util:
- package reg;
- 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= strs.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= strs.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= strs.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));
- }
-
-
- }
|