Map<String,Object>类型转json字符串,所有格式都替换成String类型

public static String map2Json(Map map) {
		if (map == null) {
			return "{}";
		} else {
			Set keys = map.keySet();
			StringBuffer jsonBuffer = new StringBuffer();
			jsonBuffer.append("{");
			Iterator it = keys.iterator();

			while (true) {
				while (it.hasNext()) {
					Object key = it.next();
					Object value = map.get(key);
					if (value instanceof List) {
						List jsonMap2 = (List) value;
						jsonBuffer.append("\"" + key + "\":[");
						Iterator iterator = jsonMap2.iterator();

						while (iterator.hasNext()) {
							Map jsonMap1 = (Map) iterator.next();
							jsonBuffer.append(map2Json(jsonMap1));
							if (iterator.hasNext()) {
								jsonBuffer.append(",");
							}
						}

						jsonBuffer.append("]");
						if (it.hasNext()) {
							jsonBuffer.append(",");
						}
					} else if (value instanceof Map) {
						jsonBuffer.append("\"" + key + "\":");
						Map jsonMap = (Map) value;
						jsonBuffer.append(map2Json(jsonMap));
						if (it.hasNext()) {
							jsonBuffer.append(",");
						}
					} else {
						jsonBuffer.append("\"" + key + "\":\""
								+ (null != value && !"null".equals(value) ? string2Json(String.valueOf(value)) : "")
								+ "\"");
						if (it.hasNext()) {
							jsonBuffer.append(",");
						}
					}
				}

				jsonBuffer.append("}");
				return jsonBuffer.toString();
			}
		}
	}

 

你可能感兴趣的:(java,util)