jmeter jsr223 javascript 深度比对json object

Life is short , play more!
本文来自lihao's Blog,转载请注明。

      
            function sortJSON(data, key, way) {

                //log.info(" " + key + " ------------------- " + typeof(key));
                return data.sort(function(a, b) {
                    var x = a[key]; var y = b[key];
                    if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }
                    if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }
                });
                
            }

            function isArray(what) {
                return Object.prototype.toString.call(what) === '[object Array]';
            }

            var deepequals = function ( x, y ) {
              if ( x === y ) return true;
                // if both x and y are null or undefined and exactly the same

              if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) return false;
                // if they are not strictly equal, they both need to be Objects

              if ( x.constructor !== y.constructor ) return false;
                // they must have the exact same prototype chain, the closest we can do is
                // test there constructor.
               // log.info("1111111111111111111111 "+ typeof("payMethod") + "" + Object.keys(x)[0] + "" + typeof(Object.keys(x)[0]));

            var sortkey2  = "payMethod";

            if(isArray(x) && x.length > 0 && typeof x[0] == 'object' &&  Object.keys(x[0])[0] === "payMethod" ) {
                
                //log.info(JSON.stringify(x)+" ======== "+ Object.keys(x));  
                //log.info(JSON.stringify(y)+" ======== "+ Object.keys(y));  
                sortkey2  = Object.keys(x[0])[0]; 
                
             if(isArray(x))   { sortJSON(x,sortkey2, '123');}
             if(isArray(y))   {	sortJSON(y,sortkey2, '123'); }
             
                //log.info(JSON.stringify(x)+" ======== "+ Object.keys(x));  
                //log.info(JSON.stringify(y)+" ======== "+ Object.keys(y));  

                }

            //if(sortkey2 == sortkey) log.info("same string " + sortkey);

            // if(isArray(x))    sortJSON(x,'a', '123');
            // if(isArray(y))	sortJSON(y,'a', '123');
                
              for ( var p in x ) {

             // 	log.info(p+":"+ JSON.stringify(x[p]) + "  ======  " +  JSON.stringify(y[p]));
                
                if ( ! x.hasOwnProperty( p ) ) continue;
                  // other properties were tested using x.constructor === y.constructor

                if ( ! y.hasOwnProperty( p ) ) { log.info(JSON.stringify(x)+"\n================属性不一致 ===============\n"+JSON.stringify(y));  return false; }
                  // allows to compare x[ p ] and y[ p ] when set to undefined

                if ( x[ p ] === y[ p ] ) continue;
                  // if they have the same strict value or identity then they are equal

                if ( typeof( x[ p ] ) !== "object" ) {  log.info(JSON.stringify(x)+"\n================值不一致===============\n"+JSON.stringify(y));  return false; };
                  // Numbers, Strings, Functions, Booleans must be strictly equal

                if ( ! deepequals( x[ p ],  y[ p ] ) ) return false;

                  // Objects and Arrays must be tested recursively
              }

              for ( p in y ) {
                if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) )  {  log.info(JSON.stringify(x)+"\n================反向检查属性不一致===============\n"+JSON.stringify(y));  return false; };
                  // allows x[ p ] to be set to undefined
              }
              return true;
            }

            //Object.prototype.deepEquals = deepEquals;

            var beforeObject = JSON.parse(vars.get("83response")); 

            var afterObject = JSON.parse(prev.getResponseDataAsString());

            if(deepequals( beforeObject, afterObject )){

                log.info("deep equals");
             
            }
            else{
                //	log.info(deepequals(testa,testb));
                AssertionResult.setFailure(true);
                AssertionResult.setFailureMessage("content not  deep equal ");
                AssertionResult.setResultForFailure("content not deep equal");
            }

 


一条评论

  1. 笑八达说道:

    感受学习的力量!

发表评论

电子邮件地址不会被公开。 必填项已用*标注