【无标题】

将json中的空去掉,并将json属性名改名

Map keyMap = new HashMap();
public static JSONObject changeJsonObj(JSONObject jsonObj,Map keyMap) {
	JSONObject resJson = new JSONObject();
	Set keySet = jsonObj.keySet();
	for (String key : keySet) {
		String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
		try {
			JSONObject jsonobj1 = jsonObj.getJSONObject(key);
			resJson.put(resKey, changeJsonObj(jsonobj1, keyMap));
		} catch (Exception e) {
			try {
				JSONArray jsonArr = jsonObj.getJSONArray(key);
				resJson.put(resKey, changeJsonArr(jsonArr, keyMap));
			} catch (Exception x) {
				resJson.put(resKey, jsonObj.get(key));
			}
		}
	}
	return resJson;
}

public static JSONArray changeJsonArr(JSONArray jsonArr,Map keyMap) {
	JSONArray resJson = new JSONArray();
	for (int i = 0; i < jsonArr.size(); i++) {
		JSONObject jsonObj = jsonArr.getJSONObject(i);
		resJson.add(changeJsonObj(jsonObj, keyMap));
	}
	return resJson;
}

你可能感兴趣的:(java,开发语言)