@Test public void mapToJsonString_shouldReturnValidMap() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("key", "value"); String expectedJson = "{\"key\":\"value\"}"; String actualJson = Json.mapToJsonString(map); assertThat(actualJson).isEqualTo(expectedJson); }
public static String mapToJsonString(Map<String, String> map) { if (map == null) { return "{}"; } StringBuilder builder = new StringBuilder(); builder.append("{"); boolean first = true; for (Map.Entry<String, String> entry : map.entrySet()) { if (!first) { builder.append(","); } builder.append("\""); builder.append(entry.getKey()); builder.append("\":\""); builder.append(entry.getValue()); builder.append("\""); first = false; } builder.append("}"); return builder.toString(); }
Json { public static String mapToJsonString(Map<String, String> map) { if (map == null) { return "{}"; } StringBuilder builder = new StringBuilder(); builder.append("{"); boolean first = true; for (Map.Entry<String, String> entry : map.entrySet()) { if (!first) { builder.append(","); } builder.append("\""); builder.append(entry.getKey()); builder.append("\":\""); builder.append(entry.getValue()); builder.append("\""); first = false; } builder.append("}"); return builder.toString(); } }
Json { public static String mapToJsonString(Map<String, String> map) { if (map == null) { return "{}"; } StringBuilder builder = new StringBuilder(); builder.append("{"); boolean first = true; for (Map.Entry<String, String> entry : map.entrySet()) { if (!first) { builder.append(","); } builder.append("\""); builder.append(entry.getKey()); builder.append("\":\""); builder.append(entry.getValue()); builder.append("\""); first = false; } builder.append("}"); return builder.toString(); }  }
Json { public static String mapToJsonString(Map<String, String> map) { if (map == null) { return "{}"; } StringBuilder builder = new StringBuilder(); builder.append("{"); boolean first = true; for (Map.Entry<String, String> entry : map.entrySet()) { if (!first) { builder.append(","); } builder.append("\""); builder.append(entry.getKey()); builder.append("\":\""); builder.append(entry.getValue()); builder.append("\""); first = false; } builder.append("}"); return builder.toString(); }  static Map<String, String> jsonStringToMap(String jsonParams); static String mapToJsonString(Map<String, String> map); static String[] jsonArrayToStringArray(String jsonString); @SuppressWarnings("unused") static T getJsonValue(final JSONObject jsonObject, final String key, final Class<T> valueClass); }
Json { public static String mapToJsonString(Map<String, String> map) { if (map == null) { return "{}"; } StringBuilder builder = new StringBuilder(); builder.append("{"); boolean first = true; for (Map.Entry<String, String> entry : map.entrySet()) { if (!first) { builder.append(","); } builder.append("\""); builder.append(entry.getKey()); builder.append("\":\""); builder.append(entry.getValue()); builder.append("\""); first = false; } builder.append("}"); return builder.toString(); }  static Map<String, String> jsonStringToMap(String jsonParams); static String mapToJsonString(Map<String, String> map); static String[] jsonArrayToStringArray(String jsonString); @SuppressWarnings("unused") static T getJsonValue(final JSONObject jsonObject, final String key, final Class<T> valueClass);  }