second, SPRINGMVC receive JSON strings in different formats
2. Continue testing SPRINGMVC receive JSON strings in different formats
1). format one: JSON simple Array Object
Two ways to transfer the front desk:
Mode one (need to stitch json strings):
1Test =function () {2 varTest = ' [{' userName ': ' test11 ', ' address ': ' gz11 '}, ' +3' {' userName ': ' ququ22 ', ' address ': ' gr22 '}] ';4 5 Jquery.ajax ({6url:cur_url+ "/weekly/test", 7Type: ' Post ', 8 data:test,9DataType: ' JSON ',TenContentType: ' Application/json;charset=utf-8 ', oneSuccess:function(data, Textstatus) { a Console.info (data); - Console.info (data.length); - for(vari = 0; I < data.length; i++) { theConsole.info (i + ":" +data[i].address); -Console.info (i + ":" +data[i].username); - } -Alert ("test success!"); + }, -Errorfunction(){ +Alert ("test error!"); a } at }); -};
Mode two (using json.stringify to convert the JSON object to a string, it is recommended to use this method, this method needs to first var a JS object, and then to the JS object plus attributes ):
1Test =function () {2 3 varTest = [{"userName": "test", "address": "gz"}, 4{"userName": "ququ", "address": "gr"} 5 ];6 7 8 Jquery.ajax ({9url:cur_url+ "/weekly/test", TenType: ' Post ', one data:JSON.stringify (test), aDataType: ' JSON ', -Success:function(data, Textstatus) { - Console.info (data); theAlert ("test success!"); - }, -Errorfunction(){ -Alert ("test error!"); + } - }); +};
There are two ways to pass a JSON string, regardless of the format of the JSON string (in This format a description, the following chapter unified use two Pass)
Background receive:
1@RequestMapping ("/test")2 @ResponseBody3 publicList<user>Test (@RequestBody user[] T) {4 for(User User:t) {5System.out.println ("user:" +user);6System.out.println ("userName:" +user.getusername ());7System.out.println ("address:" +user.getaddress ());8 9 }TenList<user> TT =arrays.aslist (t); one for(inti = 0; I < tt.size (); i++) { aUser U =Tt.get (i); -System.out.println (i + "tt:" +u); - } the returntt; - -}
This example has been mentioned before, here as an example of format;
SPRINGMVC binding json parameter bis (2.2.1)