Convert list and map to JSON using Fastjson

Source: Internet
Author: User

JSON is a good thing, is a third-party library too many have wood. Today in the conversion list into JSON, the method found on the Internet, the use of a variety of third-party libraries, to stun themselves, in fact, is very simple. So when doing things must be clear thinking, import of the package is also very important, do not come up to see the code, the whole circle of their own ... Before the use of Google's Gson, this time with Ali's Fastjson.

The project uses a servlet.

//ensure that the JSP and servlet are encoded in the same wayResp.setcontenttype ("TEXT/HTML;CHARSET=GBK"); List<String> jymdlist =NewArraylist<string>(); PrintWriter out=NULL; Try {        //get list from databaseJymdlist =Efileviewuiservice.getlymd (); //this sentence on the conversion, tossing me for a long time, simple rough, for map This sentence also appliesString JSON =json.tojsonstring (jymdlist); //get flow to JSP pass dataPrintWriter out =Resp.getwriter ();    Out.print (JSON); } Catch(baseexception e) {e.printstacktrace (); } finally{out.close (); }            

followed by a JSP, with jquery

 $ (function   () {$.ajax ({                URL:  "<%=basepath%>/servlet?cmd=getjymd&random=" +math.random (), Type:  "POST" " JSON ", success:  function   (data) { $.each (data,  function   {$ (name, value) {$ ( "#remark"  <option> +value  + "</option>" );                });        }            }); });

The cmd after the URL question mark is req.getparameter ("cmd") in the servlet and then judged, in determining which method to invoke.

After that, the jquery each corresponds to a for loop, and a select selects the value into option.

Convert list and map to JSON using Fastjson

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.