Android Studio Phase 24th-Gson Package Utils

Source: Internet
Author: User

The code has been sorted out with the following effects:

Json.utils:

Import com.google.gson.gson;import com.google.gson.gsonbuilder;import com.google.gson.jsonarray ;import com.google.gson.jsonelement;import com.google.gson.jsonobject;import  com.google.gson.jsonparseexception;import com.google.gson.jsonparser;import  com.haiersmart.utilslib.data.stringutil;import org.json.jsonarray;import org.json.jsonexception; Import org.json.jsonobject;import java.util.arraylist;import java.util.list;public class  JsonUtils {   /**  Get Gson Instances  **/   private static  Gson getinstance ()  {      gsonbuilder gsonbuilder = new  gsonbuilder ();       gsonbuilder.setdateformat ("Yyyy-MM-dd HH:mm:ss");       gsonbuilder.registertypeadapter (object.class, new  Naturaldeserializer ());       gson gson = gsonbuilder.create ();       return gson;   }    /**    *  Parse Collection     *      *  @param  json    *              Data     *  @param  beanClass    *              generics     *  @return   Generic Collections     */   public static <T> List<T>  Getbeanlist (String json, class<t> beanclass)  {       Return getbeanlist (Json, null, beanclass);   }   /**     *  parsing collections     *     *  @param  json     *  @param   Key    *            json Data Key     *  @param  beanClass    *              generics     *  @return   Generic Collections      */   public static <t> list<t> getbeanlist (String  Json, string key, class<t> beanclass)  {       Jsonparser parser = new jsonparser ();       gson gson  = getinstance ();      jsonelement contentelement =  Null;      list<t> elementlist = new arraylist<t > ();//    try {//        @SuppressWarnings (" Unused ")//   &nbsP;   jsonobject object = new jsonobject (JSON);//    }  catch  (JSONEXCEPTION&NBSP;E1)  {//       return  elementlist;//    }      if  (StringUtil.isBlank (key)  | |  stringutil.isblank (JSON))  {         contentelement  = parser.parse (JSON). Getasjsonarray ();      } else {          jsonobject jsonobject = parser.parse (JSON). Getasjsonobject ();         contentelement =  Jsonobject.get (key);      }      if  ( Stringutil.isblank (contentelement))          return  elementlist;      if  (Contentelement.isjsonarray ())  {         jsonarray  jsonarray = contentelement.getasjsonarray ();          try {            for  (int i  = 0; i < jsonarray.size ();  i++)  {                jsonelement jsonobj = jsonarray.get (i);                t entity =  gson.fromjson (Jsonobj, beanclass);                elementlist.add (Entity);             }         } catch  (classcastexception  e) &NBSP;{&NBSP;&NBSP;&NBsp;      } catch  (jsonparseexception e)  {          }         return  elementlist;      }      return elementlist;    }   /**    *  parsing a single bean    *      *  @param  json    *  @param  clazz     *  @return   Generics     */   public static <t > t getbean (String json, final class<t> clazz)  {       return getbean (json, null, clazz);    }   /**     *  parsing a single bean    *     *  @param  json    *  @param  key    *              parsed JSON data key    *  @param  clazz    *  @return   Generics     */    @SuppressWarnings ("unchecked")    public  static <t> t getbean (String json, string key, final class <t> clazz)  {      JsonParser parser = new  Jsonparser ();       gson gson = getinstance ();       jsonelement contentelement = null;      try  {          @SuppressWarnings ("unused")           jsonobject object = new jsonobject (JSON);       } catch  (JSONEXCEPTION&NBSP;E1)  {         return  null;      }      if  (StringUtil.isBlank (key) )  {         contentelement = parser.parse (JSON). Getasjsonobject ();      } else {          jsonobject jsonobject = parser.parse (JSON). Getasjsonobject ();          contentelement = jsonobject.get (Key);       }      if  (Stringutil.isblank (contentelement))           return null;      if  ( Contentelement.isjsonprimitive ())  {         if  (clazz  == string.class) &nbsp {            return  (T)   Contentelement.getasstring ();         } else if  ( Clazz == integer.class)  {             return  (T)  integer.valueof (contentelement.getasstring ());          } else if  (Clazz == number.class)  {             return  (T)  contentelement.getasnumber ();          }      } else if  ( Contentelement.isjsonobject ())  {         JsonObject  Contentobj = contentelement.getasjsonobject ();          t content = null;         try {             content = gson.fromjson (Contentobj, clazz);          } catch  (classcastexception e)  {          } catch  (jsonparseexception e)  {             system.out.println (E.getstacktrace (). toString ());          }         return  content;      }      return null;    }   /**    *  parsing related fields according to key      *      *  @param  json    *  @param  key     *  @return  &nbsP;  */   public static object getobjectbykey (String json,  String key, string targetkey)  {      jsonparser parser  = new jsonparser ();      jsonelement jsonelement;       try {         JsonObject  Jsonobject = parser.parse (JSON). Getasjsonobject ();          jsonelement = jsonobject.get (key);          return  jsonelement.getasjsonobject (). Get (Targetkey). toString ();       } catch   (exception e)  {         return null;       }   }   /** jsonobject generates a JSON string  **/    public&nBsp;static string getjsonstringbyjsonobject (Jsonobject jsonobject)  {       String s;      if  (Jsonobject == null)          s =  "";       Else         s = jsonobject.tostring ();       return s;   }   /**  parsing Jsonobject json Strings  **/   public static jsonobject getjsonobjectbyjsonstring (STRING&NBSP;STR)  {      str = str.replace ("\ufeff",  "");       JSONObject jsonobject;      if  (str !=  NULL)          try {             jSonobject = new jsonobject (str);         }  catch  (JSONEXCEPTION&NBSP;_EX)  {             jsonobject = null;         }       else         jsonobject = null;       return jsonobject;   }   /**  Analytic Jsonarray  json string  **/   public static jsonarray getjsonarraybyjsonstring (string &NBSP;STR)  {      JSONArray jsonarray;       if  (str != null)          try {             jsonarray = new jsonarray (str ); &NBSP;&NBSP;&NBSP;&Nbsp;     } catch  (JSONEXCEPTION&NBSP;_EX)  {             jsonarray = null;          }      else          jsonarray = null;      return jsonarray;    }}

Summary: Now the project is Fastjson and Gson together, we need to package well util convenient call ~


This article is from the "Liangxiao Technology Center" blog, please be sure to keep this source http://liangxiao.blog.51cto.com/3626612/1879510

Android Studio Phase 24th-Gson Package Utils

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.