code: public static JSONObject toJsonObject(Map<String, ?> map) {
      JSONObject jsonObject = new JSONObject();
      for (Map.Entry<String, ?> entry : map.entrySet()) {
        Object value = wrap(entry.getValue());
        try {
          jsonObject.put(entry.getKey(), value);
        } catch (JSONException ignored) {
          // Ignore values that JSONObject doesn't accept.
        }
      }
      return jsonObject;
    }
docs: Return a copy of the contents of the given map as a {@link JSONObject}. Instead of failing on
  {@code null} values like the {@link JSONObject} map constructor, it cleans them up and
  correctly converts them to {@link JSONObject#NULL}.