Utils { static String toJsonStr(final Map<String, String> map) { if (null == map || map.isEmpty()) { return null; } final StringBuilder sb = new StringBuilder(); sb.append('{'); for (final Map.Entry<String, String> entry : map.entrySet()) { sb.append('"') .append(entry.getKey().replace("\"", "\\\"")) .append('"') .append(':') .append('"') .append(entry.getValue().replace("\"", "\\\"")) .append('"') .append(','); } sb.deleteCharAt(sb.length() - 1); sb.append('}'); return sb.toString(); }    }
@Test public void toJsonStr() { final Map<String, String> map = new HashMap<>(); map.put("k1", "v1"); map.put("k2", "v2"); map.put("k3", "v3"); map.put("k\"", "v\""); assertEquals("{\"k1\":\"v1\",\"k2\":\"v2\",\"k\\\"\":\"v\\\"\",\"k3\":\"v3\"}", Utils.toJsonStr(map)); }