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