### Question:
QueryUtil { public static Map<String, Object> toMap(JSONObject object) throws JSONException { Map<String, Object> map = new HashMap<>(); Iterator<String> keysItr = object.keys(); while (keysItr.hasNext()) { String key = keysItr.next(); Object value = object.get(key); if (value instanceof JSONArray) { value = toList((JSONArray) value); } if (value instanceof JSONObject) { value = toMap((JSONObject) value); } map.put(key, value); } return map; } private  QueryUtil(); static JSONObject extractAggregationValue(SearchResponse response); static ArrayList toList(JSONArray array); static Map<String, Object> toMap(JSONObject object); static JSONObject extractAggregationStr(String response);  }### Answer:
@Test public void toMap() { JSONObject obj = new JSONObject(); obj.put("aggregations", "{\n" + "\"count\":{\n" + "\"value\" : 0.0\n" + "}\n" + "}"); obj.put("gender", "g_f"); obj.put("name", "obj"); JSONArray arr = new JSONArray(); JSONObject obj2 = new JSONObject(); obj2.put("gender", "g_f"); obj2.put("name", "obj2"); JSONObject obj3 = new JSONObject(); arr.put(obj2); obj3.put("arr", arr); JSONObject obj4 = new JSONObject(); obj4.put("obj", obj2); Map res = QueryUtil.toMap(obj); assertNotNull(res); res = QueryUtil.toMap(obj2); assertEquals("{gender=g_f, name=obj2}", res.toString()); res = QueryUtil.toMap(obj3); assertEquals("[{gender=g_f, name=obj2}]", res.get("arr").toString()); res = QueryUtil.toMap(obj4); assertNotNull(res.get("obj")); }