During the recent project development process, I encountered the problem of converting the front-end js objects into java objects in the background many times. Record the method for later use.
To put it simply, use the JSON.stringify() method in the frontend to convert the js object into a js string, and receive the json string in the background and convert it into a javaBean.
Frontend code:
var data = {};
data.id = $('#id').val();
data.msg = $('#msg').val();
//Submit data
$.post(contextPath '/XXX.do?' new Date().getTime(),{data: JSON.stringify(data)},function(result){
alert(result);
});
Backend code:
@RequestMapping("/XXX")
public void save(HttpservletResponse response,String data){
if(!StringUtils.isEmpty(data)){
//json Convert string to javaBean
Msg msg = (Msg) JSONObject.toBean(JSONObject.fromObject(data),Msg.class);
......
}
}