Android輕量級JSON操作類

來源:互聯網
上載者:User
package com.google.test;import java.lang.reflect.Array;import java.lang.reflect.Field;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.Collection;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.json.JSONStringer;/** JSON序列化輔助類 **/public class JSONHelper {/** 將對象轉換成Json字串 **/public static String toJSON(Object obj) {JSONStringer js = new JSONStringer();serialize(js, obj);return js.toString();}/** 序列化為JSON **/private static void serialize(JSONStringer js, Object o) {if (isNull(o)) {try {js.value(null);} catch (JSONException e) {e.printStackTrace();}return;}Class<?> clazz = o.getClass();if (isObject(clazz)) { // 對象serializeObject(js, o);} else if (isArray(clazz)) { // 數組serializeArray(js, o);} else if (isCollection(clazz)) { // 集合Collection<?> collection = (Collection<?>) o;serializeCollect(js, collection);} else { // 單個值try {js.value(o);} catch (JSONException e) {e.printStackTrace();}}}/** 序列化數組 **/private static void serializeArray(JSONStringer js, Object array) {try {js.array();for (int i = 0; i < Array.getLength(array); ++i) {Object o = Array.get(array, i);serialize(js, o);}js.endArray();} catch (Exception e) {e.printStackTrace();}}/** 序列化集合 **/private static void serializeCollect(JSONStringer js, Collection<?> collection) {try {js.array();for (Object o : collection) {serialize(js, o);}js.endArray();} catch (Exception e) {e.printStackTrace();}}/** * 序列化對象 *  * **/private static void serializeObject(JSONStringer js, Object obj) {try {js.object();for (Field f : obj.getClass().getFields()) {Object o = f.get(obj);js.key(f.getName());serialize(js, o);}js.endObject();} catch (Exception e) {e.printStackTrace();}}/** * 還原序列化簡單對象 *  * @throws **/public static <T> T parseObject(JSONObject jo, Class<T> clazz) {if (clazz == null || isNull(jo)) {return null;}T obj = createInstance(clazz);if (obj == null) {return null;}for (Field f : clazz.getFields()) {setField(obj, f, jo);}return obj;}/** * 還原序列化簡單對象 *  * @throws **/public static <T> T parseObject(String jsonString, Class<T> clazz) {if (clazz == null || jsonString == null || jsonString.length() == 0) {return null;}JSONObject jo = null;try {jo = new JSONObject(jsonString);} catch (JSONException e) {e.printStackTrace();}if (isNull(jo)) {return null;}return parseObject(jo, clazz);}/** * 還原序列化數組對象 *  * @throws **/public static <T> T[] parseArray(JSONArray ja, Class<T> clazz) {if (clazz == null || isNull(ja)) {return null;}int len = ja.length();@SuppressWarnings("unchecked")T[] array = (T[]) Array.newInstance(clazz, len);for (int i = 0; i < len; ++i) {try {JSONObject jo = ja.getJSONObject(i);T o = parseObject(jo, clazz);array[i] = o;} catch (JSONException e) {e.printStackTrace();}}return array;}/** * 還原序列化數組對象 *  * @throws **/public static <T> T[] parseArray(String jsonString, Class<T> clazz) {if (clazz == null || jsonString == null || jsonString.length() == 0) {return null;}JSONArray jo = null;try {jo = new JSONArray(jsonString);} catch (JSONException e) {e.printStackTrace();}if (isNull(jo)) {return null;}return parseArray(jo, clazz);}/** * 還原序列化泛型集合 *  * @throws **/@SuppressWarnings("unchecked")public static <T> Collection<T> parseCollection(JSONArray ja, Class<?> collectionClazz,Class<T> genericType) {if (collectionClazz == null || genericType == null || isNull(ja)) {return null;}Collection<T> collection = (Collection<T>) createInstance(collectionClazz);for (int i = 0; i < ja.length(); ++i) {try {JSONObject jo = ja.getJSONObject(i);T o = parseObject(jo, genericType);collection.add(o);} catch (JSONException e) {e.printStackTrace();}}return collection;}/** * 還原序列化泛型集合 *  * @throws **/public static <T> Collection<T> parseCollection(String jsonString, Class<?> collectionClazz,Class<T> genericType) {if (collectionClazz == null || genericType == null || jsonString == null|| jsonString.length() == 0) {return null;}JSONArray jo = null;try {jo = new JSONArray(jsonString);} catch (JSONException e) {e.printStackTrace();}if (isNull(jo)) {return null;}return parseCollection(jo, collectionClazz, genericType);}/** 根據類型建立對象 **/private static <T> T createInstance(Class<T> clazz) {if (clazz == null)return null;T obj = null;try {obj = clazz.newInstance();} catch (Exception e) {e.printStackTrace();}return obj;}/** 設定欄位的值 **/private static void setField(Object obj, Field f, JSONObject jo) {String name = f.getName();Class<?> clazz = f.getType();try {if (isArray(clazz)) { // 數組Class<?> c = clazz.getComponentType();JSONArray ja = jo.optJSONArray(name);if (!isNull(ja)) {Object array = parseArray(ja, c);f.set(obj, array);}} else if (isCollection(clazz)) { // 泛型集合// 擷取定義的泛型型別Class<?> c = null;Type gType = f.getGenericType();if (gType instanceof ParameterizedType) {ParameterizedType ptype = (ParameterizedType) gType;Type[] targs = ptype.getActualTypeArguments();if (targs != null && targs.length > 0) {Type t = targs[0];c = (Class<?>) t;}}JSONArray ja = jo.optJSONArray(name);if (!isNull(ja)) {Object o = parseCollection(ja, clazz, c);f.set(obj, o);}} else if (isSingle(clazz)) { // 實值型別Object o = jo.opt(name);if (o != null) {f.set(obj, o);}} else if (isObject(clazz)) { // 對象JSONObject j = jo.optJSONObject(name);if (!isNull(j)) {Object o = parseObject(j, clazz);f.set(obj, o);}} else {throw new Exception("unknow type!");}} catch (Exception e) {e.printStackTrace();}}/** 判斷對象是否為空白 **/private static boolean isNull(Object obj) {if (obj instanceof JSONObject) {return JSONObject.NULL.equals(obj);}return obj == null;}/** 判斷是否是實值型別 **/private static boolean isSingle(Class<?> clazz) {return isBoolean(clazz) || isNumber(clazz) || isString(clazz);}/** 是否布爾值 **/public static boolean isBoolean(Class<?> clazz) {return (clazz != null)&& ((Boolean.TYPE.isAssignableFrom(clazz)) || (Boolean.class.isAssignableFrom(clazz)));}/** 是否數值 **/public static boolean isNumber(Class<?> clazz) {return (clazz != null)&& ((Byte.TYPE.isAssignableFrom(clazz)) || (Short.TYPE.isAssignableFrom(clazz))|| (Integer.TYPE.isAssignableFrom(clazz))|| (Long.TYPE.isAssignableFrom(clazz))|| (Float.TYPE.isAssignableFrom(clazz))|| (Double.TYPE.isAssignableFrom(clazz)) || (Number.class.isAssignableFrom(clazz)));}/** 判斷是否是字串 **/public static boolean isString(Class<?> clazz) {return (clazz != null)&& ((String.class.isAssignableFrom(clazz))|| (Character.TYPE.isAssignableFrom(clazz)) || (Character.class.isAssignableFrom(clazz)));}/** 判斷是否是對象 **/private static boolean isObject(Class<?> clazz) {return clazz != null && !isSingle(clazz) && !isArray(clazz) && !isCollection(clazz);}/** 判斷是否是數組 **/public static boolean isArray(Class<?> clazz) {return clazz != null && clazz.isArray();}/** 判斷是否是集合 **/public static boolean isCollection(Class<?> clazz) {return clazz != null && Collection.class.isAssignableFrom(clazz);}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.