JSON format conversion

Source: Internet
Author: User

JSON Data Transformation

import java.io.ioexception;import java.io.stringwriter;import java.util.arraylist;import  Java.util.collections;import java.util.linkedhashmap;import java.util.list;import java.util.map ;import org.apache.commons.lang3.stringutils;import org.codehaus.jackson.jsonfactory;import  org.codehaus.jackson.jsongenerator;import org.codehaus.jackson.jsonparseexception;import  org.codehaus.jackson.map.deserializationconfig.feature;import  org.codehaus.jackson.map.jsonmappingexception;import org.codehaus.jackson.map.objectmapper;import  org.codehaus.jackson.type.typereference;import org.json.jsonarray;import org.json.jsonexception; import org.json.jsonobject;public class jsonparse {    /**      *  convert to JSON data      *      * @ param obj     *  @return       */    public static string getjson (Object obj)  {         if  (obj == null)              return null;         Objectmapper om = new objectmapper ();         Stringwriter sw = new stringwriter ();         try  {            jsongenerator jg =  new jsonfactory (). Createjsongenerator (SW);             om.writevalue (jg, obj);             jg.close ();        } catch  (IOException e)  {             e.printstacktrace ();         }        return sw.tostring ();     }     /**     *  convert JSON data to list<object>      *      *  @param  json     * @ param entityclass     *  @return      */     public static <t> list<t> getlist (String json, Class< T> entityclass)  {        if  (Stringutils.isblank ( JSON))             return null;         List<T> list = new ArrayList<T> ();      &nbSp;  objectmapper om = new objectmapper ();         om.configure (Feature.fail_on_unknown_properties, false);         try {            JSONArray  Array = new jsonarray (JSON);             for  (Int i = 0; i < array.length ();  i++)  {                 T t =  Om.readvalue (Array.getstring (i),  entityclass);                 list.add (t);             }            return list;         } catch  (jsonparseexception e)  {             e.printstacktrace ();         }  catch  (jsonmappingexception e)  {             e.printstacktrace ();        } catch  ( Ioexception e)  {             E.printstacktrace ();        } catch  (JSONException e)  {            e.printstacktrace ();         }        return null;     }    /**     *  convert JSON data to list< Linkedhashmap<string, object>>     *      *  @param  json      *  @return      */     @SuppressWarnings ("unchecked")     public static List<LinkedHashMap<String, Object>>  Getlistmap (String json)  {        if  ( Stringutils.isblank (JSON))             return  Null;        objectmapper om = new objectmapper ();         om.configure (Feature.fail_on_unknown_properties, false);         try {             List<LinkedHashMap<String, Object>> list =  Om.readvalue (json, list.class);             return list;         } catch  (jsonparseexception e)  {             e.printstacktrace ();         } catch   (jsonmappingexception e)  {             e.printstacktrace ();        } catch  (IOException  e)  {            e.printstacktrace ();         }        return null;     }    /**     *  convert JSON data to List<map <String, T>>     *      *  @param  json     *  @return      */    public static <t > list<map<string, t>> getlistmap (string json, class<t>  Entityclass)  {        if  (Stringutils.isblank (JSON))              return null;         objectmapper om = new objectmapper ();         om.configure (Feature.fail_on_unknown_properties, false);         try {            list <map<string, t>> list = om.readvalue (json, new typereference<list <Map<String, T>>> ()  {             });             return list;         } catch  (jsonparseexception e)  {             e.printstacktrace ();         }  catch  (jsonmappingexception e)  {             e.printstacktrace ();        } catch  ( Ioexception e)  {             E.printstacktrace ();        }         return null;    }    /**      *  convert JSON data to map<string, object>     *       *  @param  json     *  @return      */     @SuppressWarnings ("unchecked")     public static map<string, object> getmap (String json)  {        if  (Stringutils.isblank (JSON))              return null;         objectmapper om = new objectmapper ();         om.configure (Feature.fail_on_unknown_properties, false);         try {            map<string,  object> map = om.readvalue (Json, map.class);             return map;        }  catch  (jsonparseexception e)  {            e.printstacktrace ();         } catch  (jsonmappingexception e)  {             e.printstacktrace ();         } catch  (ioexception e)  {             e.printstacktrace ();         }         return null;    }     /**     *  convert JSON data to map<string, t>      *      *  @param  json     *  @return      */    public static <t> map<string,  t> getmap (STRING&NBSp;json, class<t> entityclass)  {        if  ( Stringutils.isblank (JSON))             return  Null;        objectmapper om = new objectmapper ();         om.configure (Feature.fail_on_unknown_properties, false);         try {             map<string, t> list = om.readvalue (json, new  Typereference<map<string, t>> ()  {             });            return list;         } catch  (jsonparseexception e)  {      &Nbsp;      e.printstacktrace ();         }  catch  (jsonmappingexception e)  {             e.printstacktrace ();        } catch  ( Ioexception e)  {             E.printstacktrace ();        }         return null;    }    /**      *  convert JSON data to a specified type      *      *  @param  json     *  @param  valuetype     * @ return     */    public static <t> t  GetObject (string json, class<t>&Nbsp;valuetype)  {        if  (Stringutils.isblank (JSON))             return null;         objectmapper om = new objectmapper ();         om.configure (Feature.fail_on_unknown_properties, false);         try {             return om.readvalue (Json, valuetype);        }  catch  (jsonparseexception e)  {             e.printstacktrace ();        } catch  ( Jsonmappingexception e)  {             E.printstacktrace ();         } catch  (ioexception e)  {             e.printstacktrace ();        }         return null;    }}


In Basecontrol, you can write a method like this.

    /**     *  Output HTML Script      */     protected void writejson (Httpservletresponse response, object obj)  {        this.writejson (response, obj,  "Utf-8");     }    /**     *  output HTML script with formatting       */    protected void writejson (HttpServletResponse  Response, object obj, string charset)  {         string json = jsonparse.getjson (obj);         Response.setcontenttype ("text/html;charset="  + charset);         response.setdateheader ("Expires",  -10);         printwriter  out = null;        try {             out = response.getwriter ();          &NBSP;&NBSP;&NBSP;OUT.PRINTLN (JSON);             Out.flush ();        } catch  (IOException e)  {             e.printstacktrace ();         } finally {             if  (out != null)                  out.close ();        }     }


When used, inherits Basecontrol, calls the Writejson method, and returns the JSON-formatted string to the foreground.

This.writejson (response, helloservice.getsomething (ID));


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.