Need to download third-party Jar:net.sf.json
Import Java.io.BufferedReader;Import Java.io.InputStream;Import Java.io.InputStreamReader;Import Java.net.URL;Import java.util.ArrayList;Import Java.util.Iterator;Import java.util.List;Import Java.util.Map;Import Java.util.Set;Import Org.apache.commons.collections.map.ListOrderedMap;Import Net.sf.json.JSONArray;Import Net.sf.json.JSONObject;/** * * A tool class that handles JSON. * <br> This class is a tool class for working with JSON *@author SLJ * *PublicClassJsontools {/** * * JSON conversion list. * <br> Detailed description *@param jsonstr JSON String *@return *@return list<map<string,object>> List *@throws *@author SLJ *@date December 24, 2013 1:08:03 * *PublicStatic list<map<string, object>>Parsejson2list (String jsonstr) {Jsonarray Jsonarr = 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; }/** * * JSON conversion map. * <br> Detailed description *@param jsonstr JSON String *@return *@return Map<string,object> Collection *@throws *@author SLJ * *PublicStatic map<string, object>Parsejson2map (String jsonstr) {Listorderedmap map =New Listorderedmap ();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 parsingif (Vinstanceof Jsonarray) {list<map<string, object>> List =New Arraylist<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; }/** * * Get JSON data over HTTP. * <br> get JSON data via HTTP return list *@param URL Link *@return *@return list<map<string,object>> List *@throws *@author SLJ * *PublicStatic list<map<string, object>>Getlistbyurl (String URL) {try {Get JSON data via HTTP 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 ();}ReturnNull }/** * * Get JSON data over HTTP. * <br> get JSON data via HTTP return map *@param URL Link *@return *@return Map<string,object> Collection *@throws *@author SLJ * *PublicStatic map<string, object>Getmapbyurl (String URL) {try {Get JSON data via HTTP 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 ();}ReturnNull }/** * * Map conversion JSON. * <br> Detailed description *@param Map Collection *@return *@return String JSON String *@throws *@author SLJ * *PublicStatic StringMaptojson (map<string, string> Map) {set<string> keys = Map.keyset (); String key =""; String value =""; StringBuffer Jsonbuffer =New StringBuffer (); Jsonbuffer.append ("{"); For (iterator<string> it = Keys.iterator (); It.hasnext ();) {key = (String) it.next (); value = Map.get (key); Jsonbuffer.append (key + ":" +"\" + value+"\" "); if (It.hasnext ()) {Jsonbuffer.append (",");}} jsonbuffer.append ("}"); return jsonbuffer.tostring ();} //test public static void main (string[] args) {String url = "http://..."; list<map<string,object>> list = Getlistbyurl (URL); SYSTEM.OUT.PRINTLN (list); }}
Java implementation string and List,map conversion