Java Web server-side processing JSON format parameters

Source: Internet
Author: User

In front of us, we talked about two kinds of books, the first is the Key-value form, the second is the JSON format,
For the first we use Request.getparameter ("key") directly on the server side, we can get the value corresponding to the key. However, you cannot use this code for the JSON that is passed.
The parameters passed by JSON exist in the body of the request, we need to read from the inside input stream, here is the code:

1 /**2 * Get the requested body3 * @paramreq4 * @return5 * @throwsIOException6 */7  Public StaticString Getrequestbody (httpservletrequest req)throwsIOException {8BufferedReader reader =Req.getreader ();9String input =NULL;TenStringBuffer requestbody =NewStringBuffer (); One      while(input = Reader.readline ())! =NULL) { A       requestbody.append (input); -     } -     returnrequestbody.tostring (); the}



StringBuffer requestbody = new StringBuffer (); Note that some use string to define Requestbody, which can have a disadvantage,
Each time Reader.readline () is not empty, requestbody + = input will create a new string object, and StringBuffer can be appended.

Java Web server-side processing JSON format parameters

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.