SPRINGMVC binding json parameter bis (2.2.1)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.