Tool class for converting JSON to map

Source: Internet
Author: User

Package Com.xxxx.util;import Java.io.bufferedreader;import Java.io.inputstream;import java.io.InputStreamReader; Import Java.net.url;import java.util.arraylist;import Java.util.hashmap;import Java.util.iterator;import Java.util.list;import Java.util.map;import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Maptools {public static list<map<string, object>> parsejson2list (String jsonstr) {Jsonarray JSON        ARR = Jsonarray.fromobject (JSONSTR);        list<map<string, object>> list = new arraylist<map<string,object>> ();        Iterator<jsonobject> it = Jsonarr.iterator ();            while (It.hasnext ()) {Jsonobject json2 = It.next ();        List.add (Parsejson2map (json2.tostring ()));    } return list; } public static map<string, object> Parsejson2map (String jsonstr) {map<string, object> Map =        New hashmap<string, object> (); Outermost parsing jsonobject json = JSOnobject.fromobject (JSONSTR);             For (object K:json.keyset ()) {Object v = json.get (k); If the inner layer is still an array, continue parsing if (v instanceof jsonarray) {list<map<string, object>> List = new A                Rraylist<map<string,object>> ();                Iterator<jsonobject> it = ((Jsonarray) v). Iterator ();                    while (It.hasnext ()) {Jsonobject json2 = It.next ();                List.add (Parsejson2map (json2.tostring ()));            } map.put (K.tostring (), list);            } else {map.put (k.tostring (), v);    }} return map; } public static list<map<string, object>> getlistbyurl (String url) {try {//via HTTP            Gets the JSON data inputstream in = new URL (URL). OpenStream ();            BufferedReader reader = new BufferedReader (new InputStreamReader (in));            StringBuilder sb = new StringBuilder (); StRing Line;            while ((Line=reader.readline ())!=null) {sb.append (line);        } return Parsejson2list (Sb.tostring ());        } catch (Exception e) {e.printstacktrace ();    } return null;            public static map<string, object> getmapbyurl (String url) {try {///HTTP GET JSON data            InputStream in = new URL (URL). OpenStream ();            BufferedReader reader = new BufferedReader (new InputStreamReader (in));            StringBuilder sb = new StringBuilder ();            String Line;            while ((Line=reader.readline ())!=null) {sb.append (line);        } return Parsejson2map (Sb.tostring ());        } catch (Exception e) {e.printstacktrace ();    } return null;        }//test public static void Main (string[] args) {String url = "http://...";        list<map<string,object>> list = Getlistbyurl (URL); System.OUT.PRINTLN (list); }}

Tool class for converting JSON to map

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.