JsonUtils

public class JsonUtil {


	// 把JsonArray的字符串转换成List<Map<String, Object>>
	public static List<Map<String, Object>> parseJsonArrayStrToListForMaps(
			String jsonArrayStr) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if (jsonArrayStr != null) {
				JSONArray jsonArray = new JSONArray(jsonArrayStr);
				Map<String, Object> map = null;
				for (int j = 0; j < jsonArray.length(); j++) {
					JSONObject jsonObject = jsonArray.getJSONObject(j);
					map = parseJsonObjectStrToMap(jsonObject.toString());
					if (map != null) {
						list.add(map);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (list.size() == 0) {
			return null;
		}
		return list;
	}

	// 把JsonObject的字符串转换成Map<String, Object>
	public static Map<String, Object> parseJsonObjectStrToMap(
			String jsonObjectStr) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (jsonObjectStr != null) {
				JSONObject jsonObject = new JSONObject(jsonObjectStr);
				for (int j = 0; j < jsonObject.length(); j++) {
					Iterator<String> iterator = jsonObject.keys();
					while (iterator.hasNext()) {
						String key = iterator.next();
						Object value = jsonObject.get(key);
						map.put(key, value);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (map.size() == 0) {
			return null;
		}
		return map;
	}

	// 把JsonObject的字符串转换成java 对象
	public static <T> T parseJsonObjectStrToEntity(String jsonObjectStr,
			Class<T> clazz) {
		Map<String, Object> map = parseJsonObjectStrToMap(jsonObjectStr);
		try {
			T entity = clazz.newInstance();
			Set<String> set = map.keySet();
			if (CollectionUtils.isNotEmpty(set)) {
				for (String key : set) {
					ReflectUtil.setValue(entity, key, map.get(key));
				}
				return entity;
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 把JsonArray的字符串转换成List<entitys>
	public static <T> List<T> parseJsonArrayStrToEntitys(String jsonArrayStr,
			Class<T> clazz) throws Exception{
		List<Map<String, Object>> list = parseJsonArrayStrToListForMaps(jsonArrayStr);
		// 返回数据集合
		List<T> entitys = new ArrayList<T>();
		// 得到对象所有字段  
		if (CollectionUtils.isNotEmpty(list)) {
			for (Map<String, Object> map : list) {
				T entity = clazz.newInstance();
				Set<String> set = map.keySet();
				if (CollectionUtils.isNotEmpty(set)) {
					for (String key : set) {
						ReflectUtil.setValue(entity, key, map.get(key));
					}
					entitys.add(entity);
				}
			}
			return entitys;
		}
		return null;
	}
}


///////////////////
public class ReflectUtil {
	
	/**
	 * 获取实体类一个成员变量的值
	 */
	public static Object getValue(Object entity, String fieldName) {
		Object value = null;
		try {
			Class<?> clazz = entity.getClass();
			String methodName = "get" + StrUtil.getUpperCharAt(fieldName, 0);
			Method method = clazz.getMethod(methodName);
			value = method.invoke(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
	/**
	 * 设置实体类一个成员变量的值
	 */
	public static void  setValue(Object entity, String fieldName, Object value) {
		try {
			Class<?> clazz = entity.getClass();
			Field field = clazz.getDeclaredField(fieldName);
			Class<?> type = field.getType();
			String methodName = "set" + StrUtil.getUpperCharAt(fieldName, 0);//有参函数
			Method method = clazz.getMethod(methodName, type);
			method.invoke(entity, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
/////////////////////////
public class StrUtil {

	/**
	 * 把字符串的指定字母大写
	 */
	public static String getUpperCharAt(String str, int index) {
		String result = null;
		int count = str.length();
		if (count > index) {
			String start = str.substring(0, index);
			String at = (str.charAt(index) + "").toUpperCase();
			String end = str.substring(index + 1);
			result = start + at + end;
		} else {
			Log.d("", "数组越界");
		}
		return result;
	}

	/**
	 * 把字符串中的特殊字符转义符转换回特殊字符
	 */
	public static String specialCharParse(String str) {
		if (StringUtils.isNotBlank(str)) {
			str = str.replace("#1#", "%");
			str = str.replace("#2#", "&");
			// str = str.replace("#3#", "$");
		}
		return str;
	}
}
 

 

 

你可能感兴趣的:(json)