Java handles JSON's tool class (conversion between List,map and JSON) __java

Source: Internet
Author: User

Need to download a 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;
/** * * Tool class for processing JSON.
    * <br> This class is a tool class for processing JSON * @author SLJ/public class Jsontools {/** * * JSON conversion list. * <br> Detailed description * @param jsonstr JSON String * @return * @return list<map<string,object>> List * @throws * @author SLJ * @date December 24, 2013 afternoon 1:08:03 * * public static list<map<string, Object>&gt ;
        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 transforms map. * <br> Detailed description * @param jsonstr JSON String * @return * @return map<string,object> Collection * @throws * @auth or SLJ */public static map<string, object> Parsejson2map (String jsonstr) {Listorderedmap Map = new
        Listorderedmap ();
        Outermost parse 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
                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;
    /** * * gets JSON data via HTTP. 
    * <br> get JSON data via HTTP return list * @param URL link * @return * @return list<map<string,object>> List 
        * @throws * @author SLJ/public static list<map<string, object>> getlistbyurl (String URL) {
            try {//Get JSON data inputstream in = new URL (URL) via HTTP. 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; /** * * via HTTP GET JSon data.  * <br> get JSON data via HTTP return Map * @param URL link * @return * @return map<string,object> Collection * @throws * @author SLJ */public static map<string, object> getmapbyurl (String url) {try {//through HTT
            P 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 Parsejson2map (Sb.tostring ());
        catch (Exception e) {e.printstacktrace ();
    return null;
    }/** * * Map converts JSON.
    * <br> Detailed description * @param map Collection * @return * @return string JSON String * @throws * @author SLJ * * public static String Maptojson (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); }
}

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.