Android轻量级JSON操作类

本文介绍了一个用于对象到JSON字符串和JSON到对象转换的辅助类,包括对象序列化、数组序列化、集合序列化以及简单的对象、数组、集合的反序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能体格

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值