Json { public static Map<String, String> jsonStringToMap(String jsonParams) throws Exception { Map<String, String> jsonMap = new HashMap<String, String>(); if (jsonParams == null || jsonParams.equals("")) return jsonMap; JSONObject jsonObject = (JSONObject) new JSONTokener(jsonParams).nextValue(); Iterator<?> keys = jsonObject.keys(); while (keys.hasNext()) { String key = (String) keys.next(); jsonMap.put(key, jsonObject.getString(key)); } return jsonMap; }  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);  }
@Test public void jsonStringToMap_shouldParseJson() throws Exception { expectedMap.put("key", "value"); expectedMap.put("other_key", "other_value"); String json = "{\"key\":\"value\",\"other_key\":\"other_value\"}"; Map<String, String> map = Json.jsonStringToMap(json); assertThat(map).isEqualTo(expectedMap); }
@Test public void jsonStringToMap_whenStringIsNull_shouldReturnEmptyMap() throws Exception { Map<String, String> map = Json.jsonStringToMap(null); assertThat(map).isEqualTo(expectedMap); }
@Test public void jsonStringToMap_whenStringIsEmpty_shouldReturnEmptyMap() throws Exception { Map<String, String> map = Json.jsonStringToMap(""); assertThat(map).isEqualTo(expectedMap); }