Java parsing JSON and map,list conversions to each other

Source: Internet
Author: User

Running this class requires loading the jar package: Ezmorph-1.0.6.jar, Json-lib-2.4-jdk15.jar, Jsoup-1.6.1.jar, Commons-beanutils-1.8.0.jar, Commons-collectio.jar, Commons-lang-2.4.jar, Commons-logging-1.1.1.jar.
Click here to download the required Clip Pack http://download.csdn.net/detail/weizunde/7233825

 Packagecom.jjinfo.action;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.Reader;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjava.net.URLConnection;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.Date;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJavax.servlet.http.HttpServletResponse;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject;ImportOrg.apache.struts2.ServletActionContext; Public classtestaction { Public voidSendMessage (String content)throwsIOException {httpservletresponse response=Servletactioncontext.getresponse (); Response.setcharacterencoding ("UTF-8"); //response.setcharacterencoding ("GBK");response.getwriter (). write (content);} Private StaticList<map<string, string>> jsonstringtolist (String rscontent)throwsException {Jsonarray Arry=Jsonarray.fromobject (rscontent); System.out.println ("JSON string contents are as follows");         System.out.println (Arry); List<map<string, string>> rslist =NewArraylist<map<string, string>>();  for(inti = 0; I < arry.size (); i++) {Jsonobject jsonobject=Arry.getjsonobject (i); Map<string, string> map =NewHashmap<string, string>();  for(Iterator<?> iter =Jsonobject.keys (); Iter.hasnext ();) {String key=(String) iter.next (); String value=Jsonobject.get (key). ToString ();             Map.put (key, value);         } rslist.add (map); }         returnrslist; }   PublicString query ()throwsexception{String str=""; //get XML, read into XML fileSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println ("----Start----" +sdf.format (NewDate ()));            URL url; URL=NewURL ("Http://star.sgst.cn/starInfoForZSCQ.do?systemName=ZSCQ&pageNum=6"); URLConnection UC=url.openconnection (); InputStream in=Uc.getinputstream (); Reader Rd=NewInputStreamReader (In, "UTF-8"); byte[] Ch=New byte[1024]; StringBuilder SB=NewStringBuilder (); intC=0; StringBuffer Temp=NewStringBuffer ();  while((c = Rd.read ())! = 1) {temp.append (Char) c);         } in.close (); STR=temp.tostring ();        System.out.println (Temp.tostring ()); System.out.println ("----zhong----" +sdf.format (NewDate ())); List<map<string, string>> list1 =jsonstringtolist (str); System.out.println ("JSON string into map");  for(Map<string, string>m:list1)        {System.out.println (M); }         return NULL; }}

Java parsing JSON and map,list conversions to each other

Related Article

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.