android application/app/src/main/java/com/slensky/focussis/parser/DemographicParser.java
private JSONObject findField(@NonNull JSONArray result, @NonNull String title, @NonNull String id, @NonNull String columnName) throws JSONException {
        // result title
        for (int i = 0; i < result.length(); i++) {
            if (!(result.get(i) instanceof JSONObject)) {
                continue;
            }

            JSONObject field = result.getJSONObject(i);
            if (field.getString("title").equals(title)) {
                Log.v(TAG, String.format("\"%s\" retrieved via title", title));
                return field;
            }
        }

        // result id
        for (int i = 0; i < result.length(); i++) {
            if (!(result.get(i) instanceof JSONObject)) {
                continue;
            }

            JSONObject field = result.getJSONObject(i);
            if (field.getString("id").equals(id)) {
                Log.v(TAG, String.format("\"%s\" retrieved via id", title));
                return field;
            }
        }

        // result columnName
        for (int i = 0; i < result.length(); i++) {
            if (!(result.get(i) instanceof JSONObject)) {
                continue;
            }

            JSONObject field = result.getJSONObject(i);
            if (field.getString("column_name").equals(columnName)) {
                Log.v(TAG, String.format("\"%s\" retrieved via column_name", title));
                return field;
            }
        }

        throw new NoSuchElementException(String.format("Field title %s, id %s, column_name %s could not be found", title, id, columnName));
    }
// prefers to find a matching title, then matching id, then matching columnName
prefers to find a matching title, then matching id, then matching columnName
prefers to find a matching title then matching id then matching columnName
private JSONObject findField(@NonNull JSONArray result, @NonNull String title, @NonNull String id, @NonNull String columnName) throws JSONException {
        for (int i = 0; i < result.length(); i++) {
            if (!(result.get(i) instanceof JSONObject)) {
                continue;
            }
            JSONObject field = result.getJSONObject(i);
            if (field.getString("title").equals(title)) {
                Log.v(TAG, String.format("\"%s\" retrieved via title", title));
                return field;
            }
        }
        for (int i = 0; i < result.length(); i++) {
            if (!(result.get(i) instanceof JSONObject)) {
                continue;
            }
            JSONObject field = result.getJSONObject(i);
            if (field.getString("id").equals(id)) {
                Log.v(TAG, String.format("\"%s\" retrieved via id", title));
                return field;
            }
        }
        for (int i = 0; i < result.length(); i++) {
            if (!(result.get(i) instanceof JSONObject)) {
                continue;
            }
            JSONObject field = result.getJSONObject(i);
            if (field.getString("column_name").equals(columnName)) {
                Log.v(TAG, String.format("\"%s\" retrieved via column_name", title));
                return field;
            }
        }
        throw new NoSuchElementException(String.format("Field title %s, id %s, column_name %s could not be found", title, id, columnName));
    }
private JSONObject findField ( @ NonNull JSONArray result , @ NonNull String title , @ NonNull String id , @ NonNull String columnName ) throws JSONException { for ( int i = 0 ; i < result . length ( ) ; i ++ ) { if ( ! ( result . get ( i ) instanceof JSONObject ) ) { continue ; } JSONObject field = result . getJSONObject ( i ) ; if ( field . getString ( "title" ) . equals ( title ) ) { Log . v ( TAG , String . format ( "\"%s\" retrieved via title" , title ) ) ; return field ; } } for ( int i = 0 ; i < result . length ( ) ; i ++ ) { if ( ! ( result . get ( i ) instanceof JSONObject ) ) { continue ; } JSONObject field = result . getJSONObject ( i ) ; if ( field . getString ( "id" ) . equals ( id ) ) { Log . v ( TAG , String . format ( "\"%s\" retrieved via id" , title ) ) ; return field ; } } for ( int i = 0 ; i < result . length ( ) ; i ++ ) { if ( ! ( result . get ( i ) instanceof JSONObject ) ) { continue ; } JSONObject field = result . getJSONObject ( i ) ; if ( field . getString ( "column_name" ) . equals ( columnName ) ) { Log . v ( TAG , String . format ( "\"%s\" retrieved via column_name" , title ) ) ; return field ; } } throw new NoSuchElementException ( String . format ( "Field title %s, id %s, column_name %s could not be found" , title , id , columnName ) ) ; }
prefers to find a matching title, then matching id, then matching columnName
prefers to find a matching title then matching id then matching columnName
// result title // result id // result columnName