Many times in the development project, I encountered the problem of converting front-end js objects into java objects in the background. The following describes how to convert front-end js objects to back-end java objects, interested friends can learn about the problem of converting front-end js objects to java objects in the background many times during the recent project development process. Record the practices to be used.
Simply put, the front-end uses the JSON. stringify () method to convert a js object to a js string. the backend receives a json string and converts it to a javaBean.
Front-end code:
The Code is as follows:
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 );
});
Background code:
The Code is as follows:
@ RequestMapping ("/XXX ")
Public void save (HttpservletResponse response, String data ){
If (! StringUtils. isEmpty (data )){
// Convert the json string to javaBean
Msg msg = (Msg) JSONObject. toBean (JSONObject. fromObject (data), Msg. class );
......
}
}