介绍:java 解析 json字符串
支持数据格式:String、int、Integer、double、Double、boolean、Boolean、BigDecimal、List、Map
Map key 需为 String
引用数据类型的解析需要 传递映射 Map<String,Class> 其中 key为对象中的属性名
package com.json; /** * 定义 json 解析器 * Created by wangxin on 2015/2/14. */ public interface JsonParser { /** * @param jsonStr * @return */ public JsonResponse parse(String jsonStr) throws Exception; }
package com.json.parser; import com.json.JsonParser; import com.json.JsonResponse; import net.sf.json.JSONArray; import net.sf.json.JSONException; import net.sf.json.JSONObject; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * 简单对象解析, 节点均为基础数据类型/Bigdecimal 的对象 * Created by wangxin on 2015/3/12. */ public class SimpleObjectParser implements JsonParser { private static final String TAG = SimpleObjectParser.class.getSimpleName(); private Class<? extends JsonResponse> responseType; private Map<String, Class> typeClass; /** * 简单类型转换 目前支持的类型 * 支持的集合类有 List Map * Map 的 key值 必须为 string */ private static interface ClassType { String STRING = "class java.lang.String"; String INT = "int"; String INTEGER = "class java.lang.Integer"; String DOUBLE = "double"; String DOUBLE_CLASS = "class java.lang.Double"; String BOOLEAN = "boolean"; String BOOLEAN_CLASS = "class java.lang.Boolean"; String BIGDECIMAL = "java.math.BigDecimal"; } public SimpleObjectParser(Class<? extends JsonResponse> responseType) { this.responseType = responseType; } public SimpleObjectParser(Class<? extends JsonResponse> responseType, Map<String, Class> typeClass) { this.responseType = responseType; this.typeClass = typeClass; } @Override public JsonResponse parse(String jsonStr) throws Exception { try { JSONObject json = JSONObject.fromObject(jsonStr); return (JsonResponse) parse(json, responseType); } catch (Exception e) { // LogUtil.e(TAG, "pase error:" + jsonStr); throw e; } } /** * 解析, JSONObject 对应成 指定的 类型 * * @param json * @param classType * @return * @throws IllegalAccessException * @throws InstantiationException */ private Object parse(JSONObject json, Class classType) throws IllegalAccessException, InstantiationException { if (null == classType) return null; Method[] methods = classType.getMethods(); Object result = classType.newInstance(); for (Method m : methods) { if (!m.getName().startsWith("set") || m.getParameterTypes().length != 1) continue; try { Object param = parseParam(m.getParameterTypes()[0], getPropertyName(m.getName()), json); if (null != param) m.invoke(result, param); } catch (Exception e) { continue; } } return result; } /** * 从 json 对象中获取 属性值 * * @param type * @param propertyName * @param json * @return */ private Object parseParam(Class type, String propertyName, JSONObject json) throws JSONException { Object param = null; String typeStr = type.toString(); if (typeStr.equals(ClassType.STRING)) param = json.getString(propertyName); else if (typeStr.equals(ClassType.INT) || typeStr.equals(ClassType.INTEGER)) param = json.getInt(propertyName); else if (typeStr.equals(ClassType.DOUBLE) || typeStr.equals(ClassType.DOUBLE_CLASS)) param = json.getDouble(propertyName); else if (typeStr.equals(ClassType.BOOLEAN) || typeStr.equals(ClassType.BOOLEAN_CLASS)) param = json.getBoolean(propertyName); else if (typeStr.equals(ClassType.BIGDECIMAL)) param = new BigDecimal(json.getString(propertyName)); else if (List.class.isAssignableFrom(type)) param = parseList(json, propertyName, type); else if (Map.class.isAssignableFrom(type)) param = parseMap(json, propertyName, type); else if (typeStr.startsWith("class ")) param = parseObject(json, propertyName); return param; } /** * 根据 set 方法名获得属性名称 * * @param methodName * @return */ private String getPropertyName(String methodName) { methodName = methodName.substring(3); return methodName.substring(0, 1).toLowerCase() + methodName.substring(1); } /** * 解析 list * * @return */ private List parseList(JSONObject json, String propertyName, Class type) { try { JSONArray jsonArray = json.getJSONArray(propertyName); List result = type.isInterface() ? new ArrayList() : (List) type.newInstance(); Object param; for (int i = 0; i < jsonArray.size(); i++) { try { param = parse(jsonArray.getJSONObject(i), typeClass.get(propertyName)); if (null == param) param = jsonArray.get(i); result.add(param); } catch (JSONException e) { result.add(jsonArray.get(i)); } } return result; } catch (Exception e) { return null; } } /** * 解析 map * * @param json * @param propertyName * @param type * @return */ private Map parseMap(JSONObject json, String propertyName, Class type) { try { Map map = type.isInterface() ? new HashMap() : (Map) type.newInstance(); JSONObject jsonObj = json.getJSONObject(propertyName); Iterator<String> keys = jsonObj.keys(); String key; Object param; while (keys.hasNext()) { key = keys.next(); try { param = parse(jsonObj.getJSONObject(key), typeClass.get(propertyName)); if (null != param) param = jsonObj.get(key); map.put(key, param); } catch (JSONException e) { map.put(key, jsonObj.get(key)); } } return map; } catch (Exception e) { return null; } } /** * 解析 JavaBean * * @param json * @param propertyName * @return */ private Object parseObject(JSONObject json, String propertyName) { try { if (null == typeClass || typeClass.get(propertyName) == null) return null; JSONObject jsonObject = json.getJSONObject(propertyName); return parse(jsonObject, typeClass.get(propertyName)); } catch (Exception e) { return null; } } }