Android解析Json数据(二)—使用反射机制
16lz
2021-01-26
网络传输中很多时候都会使用到JSon数据,每次都要根据key去解析实在是太繁琐、而且容易出错。这个可以通过java中的反射机制来解决,写一个公用的解析类而一劳永逸。
import java.lang.reflect.Field;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.util.Log;/** * json 解析工具 * * @author Kevin * */public class JsonUtil {private static final String TAG = "jsonUtil";private JSONObject jsonObject;public void setJsonObject(JSONObject jsonObject) {this.jsonObject = jsonObject;}public JsonUtil(String json) {jsonObject = getJsonObject(json);if (jsonObject == null) {Log.e(TAG, "jsonobject is null");}}public JsonUtil() {super();}/** * 得到一个jsonObject对象 * * @param json * json string * @return JOSNObject */public static JSONObject getJsonObject(String json) {JSONObject jsonObject = null;try {jsonObject = new JSONObject(json);} catch (JSONException e) {Log.e(TAG, "create jsonobject exception");e.printStackTrace();}return jsonObject;}/** * 根据关键字Key获取String对象 * * @param json * json data * @param key * param * @return String data * @throws JSONException */public String getString(String key) throws JSONException {if (jsonObject != null) {return jsonObject.getString(key);} else {return null;}}/** * 根据关键字Key获取Int对象 * @param key * @return int * @throws JSONException */public int getInt(String key) throws JSONException {if (jsonObject != null) {return jsonObject.getInt(key);} else {return -1;}}/** * 根据关键字Key获取Double对象 * @param key * @return double * @throws JSONException */public double getDouble(String key) throws JSONException {if (jsonObject != null) {return jsonObject.getDouble(key);} else {return -1;}}/** *Key 值等于类名时 *通过反射解析得到c对象 * @param c * @return object * @throws Exception */public Object getObject(Class<?> c) throws Exception {if (jsonObject != null) {return getObject(c.getSimpleName(), c);} else {return null;}}/** * * 通过Key值反射解析得到c对象 * * @param jsonObject * @param key * query key * @param c * class * @return object * @throws Exception */public Object getObject(String key, Class<?> c) throws Exception {if (jsonObject != null) {return getObject(jsonObject, key, c);} else {return null;}}public Object getObject(JSONObject jsonObject, Class<?> c) throws Exception {return getObject(jsonObject, c.getSimpleName(), c);// return getObject(jsonObject, c.getSimpleName().toLowerCase(), c);}/** * 从jsonObject中解析Key值,得到C对象 * * @param jsonObject * @param key * query key * @param c * class * @return object * @throws Exception */public Object getObject(JSONObject jsonObject, String key, Class<?> c)throws Exception {// Log.e(TAG, "key == " + key);Object bean = null;if (jsonObject != null) {JSONObject jo = null;if (key != null) {jo = jsonObject.getJSONObject(key);} else {jo = jsonObject;}if (jo != null) {if (c.equals(null)) {Log.e(TAG, "class is null");bean = jo.get(key);} else {bean = c.newInstance();// Field[] fs = c.getFields();Field[] fs = c.getDeclaredFields();for (int i = 0; i < fs.length; i++) {Field f = fs[i];f.setAccessible(true);Type type = f.getGenericType();String value = null;//解析Listif(f.getType().equals(List.class)){String fClassName = toUpperCaseFirstOne(f.getName());String fClass = "com.example."+ fClassName;f.set(bean,getList(jo.getJSONArray(fClassName),fClassName,Class.forName(fClass)));continue;}value = jo.getString(f.getName());// Log.e(TAG, f.getName() + "=" + value);if (type.equals(int.class)) {if(value.length() == 0){f.setInt(bean, 0);}else{f.setInt(bean, Integer.valueOf(value));}} else if (type.equals(double.class)) {if(value.length() == 0){f.setDouble(bean, 0);}else{f.setDouble(bean, Double.valueOf(value));}} else {f.set(bean, value);}}}} else {Log.e(TAG, "in jsonobject not key ");}} else {Log.e(TAG, "current param jsonobject is null");}return bean;}/** *等同getList(String key ,Class<?> c) key值 =c的类名 * * @param c * @return * @throws Exception */public List<Object> getList(Class<?> c) throws Exception {return getList(c.getSimpleName(), c);}/** * 解析数组,返回一个链表 * * @param key * @param c * List<E>, * @return list * @throws Exception */public List<Object> getList(String key, Class<?> c) throws Exception {if (jsonObject != null) {JSONArray jsonArray = jsonObject.getJSONArray(key);return getList(jsonArray, key, c);} else {return null;}}/** * 从jsonArray中解析得到List * * @param jsonArray * @param key * @param c * @return * @throws Exception */public List<Object> getList(JSONArray jsonArray, String key, Class<?> c)throws Exception {List<Object> list = null;if (!jsonArray.isNull(0)) {list = new ArrayList<Object>();for (int i = 0; i < jsonArray.length(); i++) {JSONObject jsObject = jsonArray.getJSONObject(i);Object object = getObject(jsObject, null, c);list.add(object);}}return list;}/***第一个字母转化成大写**/private String toUpperCaseFirstOne(String s) {if (Character.isUpperCase(s.charAt(0)))return s;elsereturn (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();}/** * @param jsonObject * @param key * @return */public static String getString(JSONObject jsonObject,String key){String str =null; try {str = jsonObject.getString(key);} catch (JSONException e) {return null;}return str;}/** * @param jsonObject * @param key * @return */public static int getInt(JSONObject jsonObject,String key){int arg = -1; try {arg = jsonObject.getInt(key);} catch (JSONException e) {return -1;}return arg;}/** * @param jsonObject * @param key * @return */public static JSONObject getJsonObject(JSONObject jsonObject,String key){JSONObject object = null; try {object = jsonObject.getJSONObject(key);} catch (JSONException e) {return null;}return object;}/** * @param jsonObject * @param key * @return */public static JSONObject getJsonObject(String json,String key){JSONObject object = null; JSONObject jsonObject = getJsonObject(json);object = getJsonObject(jsonObject,key);return object;}/** * @param jsonObject * @param key * @return */public static JSONArray getJSONArray(JSONObject jsonObject,String key){JSONArray object = null; try {object = jsonObject.getJSONArray(key);} catch (JSONException e) {return null;}return object;}/** * @param jsonObject * @param key * @return */public static JSONArray getJSONArray(String json,String key){JSONArray object = null; JSONObject jsonObject = getJsonObject(json);object = getJSONArray(jsonObject, key);return object;}}
更多相关文章
- Appium框架解析
- Android(安卓)游戏开发之SurfaceView的简单使用
- Android属性动画---Property Animation(六)
- Android(安卓)Input系统源码分析一(启动与初始化)
- Android(安卓)Studio 视图解析
- Activity的四种加载模式 -- singleTask 和 singleInstance模式
- Android——SharedPreferences数据存储
- android NumberPicker 全面解析
- 深入init进程(and5.1)