protected void recursivelyCompareJSONArray ( String key , JSONArray expected , JSONArray actual , JSONCompareResult result ) throws JSONException { Set < Integer > matched = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < expected . length ( ) ; ++ i ) { Object expectedElement = expected . get ( i ) ; boolean matchFound = false ; for ( int j = 0 ; j < actual . length ( ) ; ++ j ) { Object actualElement = actual . get ( j ) ; if ( matched . contains ( j ) || ! actualElement . getClass ( ) . equals ( expectedElement . getClass ( ) ) ) { continue ; } if ( expectedElement instanceof JSONObject ) { if ( compareJSON ( ( JSONObject ) expectedElement , ( JSONObject ) actualElement ) . passed ( ) ) { matched . add ( j ) ; matchFound = true ; break ; } } else if ( expectedElement instanceof JSONArray ) { if ( compareJSON ( ( JSONArray ) expectedElement , ( JSONArray ) actualElement ) . passed ( ) ) { matched . add ( j ) ; matchFound = true ; break ; } } else if ( expectedElement . equals ( actualElement ) ) { matched . add ( j ) ; matchFound = true ; break ; } } if ( ! matchFound ) { result . fail ( key + "[" + i + "] Could not find match for element " + expectedElement ) ; return ; } } }
easy way to uniquely identify each element .