前端傳json物件(包含json陣列在裡面)的時候,後臺為Java,用SSM時候如何接收
阿新 • • 發佈:2019-01-07
前端JS程式碼 如下:
var url = "web";
var postdata = {"history":[
{
"time":"2017-10-18 10:10:10", //time 欄位:時間
"option":"human" //option 欄位:使用者human,機器machine
},
{
"time":"2017-10-19 99:99:99",
"option":"machine"
}
],
"openid":"abcdefghijklmn",
"type":"家庭"
};
$.ajax({
type:"POST",
url:url,
//JSON.stringify(),將postdata物件轉換成字串形式
data:{"postdata":JSON.stringify(postdata)}, //呼叫ajax的時候,data屬性必須這樣寫。必須dataType:"json",
success:function(data){
}
}
});
Java程式碼如下
//@RequestParam("postdata") 這句話是關鍵,跟前端的 data:{"postdata":JSON.stringify(postdata)} 對應,屬性名要一致
@RequestMapping("/addQuestionIntelligence")
public @ResponseBody ResponseResult addQuestionIntelligence(@RequestParam("postdata") String postdata, HttpServletRequest request,HttpSession
session)throws Exception{
org.json.JSONObject json = new org.json.JSONObject(postdata);
}