First, the server side gets the JSON string and resolves
Way One:
The front end sends a getjsonstring.spring request, which is added in time to ensure that the browser takes it as a different request each time, without invoking the browser cache.
Idea: Use Json.stringify () to convert a JS object instance [Object:object] into a JSON-formatted string. $.post () The optional parameters accepted by data can be plainobject/string
A key-value pair or string that is key/value.
<script src= "Jquery-1.3.2.js" ></script> <script src= "Json2.js" ></script> <script& Gtfunctionuserinfo (username, password) { This. Username =username; This. Password =password; } functionSendajax () {varUserinforef =NewUserInfo (' Gaohong ', ' 123 '); varJsonstringref =json.stringify (USERINFOREF); $.post ("Getjsonstring.spring?t=" +NewDate (). GetTime (), {jsonstring:jsonstringref}); } </script>
Backend code: Idea: The Jsonobject.fromobject () method converts a JSON-formatted string into a Jsonobject-Java object.
Note: The request sent by Ajax does not add the path to the current directory, and the request sent by the form submits the directory path to the file where the request was sent.
Jsonobject.fromobject () is a JSON parsing method provided by Json-lib-2.3-jdk1.5.jar.
@RequestMapping (value = "getjsonstring") public string getjsonstring (@RequestParam ("jsonstring") string jsonstring) { Jsonobject object = Jsonobject.fromobject (jsonstring); System.out.println (Object.get ("username")); System.out.println (Object.get ("password")); return "test.jsp";}
Method Two: The technique of automatically turning from a JSON-formatted string to an entity using SPRINGMVC.
SPRINGMVC Learning Note 001-server-side get JSON string and parse