public static String createJSONString ( HttpServletRequest request , String controller ) throws Exception { JSONObject obj = new JSONObject ( ) ; try { Field [ ] allFields = Class . forName ( "com.microsoft.windowsazure.activedirectory.sdk.graph.models." + controller ) . getDeclaredFields ( ) ; String [ ] allFieldStr = new String [ allFields . length ] ; for ( int i = 0 ; i < allFields . length ; i ++ ) { allFieldStr [ i ] = allFields [ i ] . getName ( ) ; } List < String > allFieldStringList = Arrays . asList ( allFieldStr ) ; Enumeration < String > fields = request . getParameterNames ( ) ; while ( fields . hasMoreElements ( ) ) { String fieldName = fields . nextElement ( ) ; String param = request . getParameter ( fieldName ) ; if ( allFieldStringList . contains ( fieldName ) ) { if ( param == null || param . length ( ) == 0 ) { if ( ! fieldName . equalsIgnoreCase ( "password" ) ) { obj . put ( fieldName , JSONObject . NULL ) ; } } else { if ( fieldName . equalsIgnoreCase ( "password" ) ) { obj . put ( "passwordProfile" , new JSONObject ( "{\"password\": \"" + param + "\"}" ) ) ; } else { obj . put ( fieldName , param ) ; } } } } } catch ( JSONException e ) { e . printStackTrace ( ) ; } catch ( SecurityException e ) { e . printStackTrace ( ) ; } catch ( ClassNotFoundException e ) { e . printStackTrace ( ) ; } return obj . toString ( ) ; }
This method would create a string consisting of a JSON document with all the necessary elements set from the HttpServletRequest request .