code: private static Object convert(Object x) throws JSONException {
      if (x instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) x;
        String[] names = JSONObject.getNames(jsonObject);
        if (names == null || names.length == 0) {
          return new HashMap<>(0);
        }
        HashMap<String, Object> map = new HashMap<>(names.length);
        for (String name : names) {
          Object value = jsonObject.get(name);
          map.put(name, convert(value));
        }
        return map;
      } else if (x instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) x;
        int length = jsonArray.length();
        List<Object> list = new ArrayList<>(length);
        for (int i = 0; i < length; i++) {
          list.add(convert(jsonArray.get(i)));
        }
        return list;
      } else {
        return x;
      }
    }
docs: Convert an <code>org.json.JSONObject</code> into a <code>Map</code> and an
  <code>org.json.JSONArray</code> into a <code>List</code>