1.首先看一下前台序列化了哪些東西:
部分js代碼
//查詢按鈕 $(".questButton").click(function(){ $.ajax({url:"/questionnaire/statistics.jhtml", type:"get", async:false, traditional:false, data:{questOptions:$("input[name='category']:checked").serialize(),condition:$(".form1 :not(input[name='category'])").serialize()}, success:function(data){
前台發送給后台的序列化的字符串是如下的樣子:

2.后台接受到就是兩個字符串,並且是編碼之后的,所以第一點先要按照編碼解析,然后再進行處理
@RequestMapping(value= "/statistics" ,produces = "text/html;charset=UTF-8") @ResponseBody public String statistics(HttpServletRequest request,String condition,String questOptions) throws UnsupportedEncodingException{ questOptions = questOptions.replaceAll("category=", ""); String [] questArr = questOptions.split("&"); condition = URLDecoder.decode(condition, "utf-8"); condition = "{"+condition.replaceAll("&", "\",").replaceAll("=", ":\"")+"\"}"; JSONObject jsonObject = JSONObject.fromObject(condition); System.out.println("JSONObject格式:"+jsonObject); System.out.println("為轉化之前的字符串:"+condition); System.out.println("另一個參數的樣子:"+questOptions); return null; }
condition = URLDecoder.decode(condition, "utf-8");
先進行編碼解析
condition = "{"+condition.replaceAll("&", "\",").replaceAll("=", ":\"")+"\"}";
替換相關的符號,拼接成標准的JSON字符串
JSONObject jsonObject = JSONObject.fromObject(condition);
轉換成JSONObject對象
3.結果如下
JSONObject格式:{"userName":"測試數據","sex":"男","age1":"20","age2":"30","height1":"","height2":"","weights1":"","weights2":"","uaValue1":"","uaValue2":"","uaphValue1":"","uaphValue2":""} 為轉化之前的字符串:{userName:"測試數據",sex:"男",age1:"20",age2:"30",height1:"",height2:"",weights1:"",weights2:"",uaValue1:"",uaValue2:"",uaphValue1:"",uaphValue2:""} 另一個參數的樣子:2.1.2&3.1.1&3.2.3&7.2.2
