Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 比较来自API的两个响应,但出现错误_Javascript_Postman - Fatal编程技术网

Javascript 比较来自API的两个响应,但出现错误

Javascript 比较来自API的两个响应,但出现错误,javascript,postman,Javascript,Postman,我有两个回应:- ResultObj32 (Response1) "errorDescription": "", "message": "", "resultCode": "OK", "resultObj": { "userLinesList": [{ "lineId": 1, "lineType": "MOBILE", "lineValue": "11" }], "externalObject": { }} Resu

我有两个回应:-

ResultObj32 (Response1)
"errorDescription": "",
"message": "",
"resultCode": "OK",
"resultObj": {
    "userLinesList": [{
        "lineId": 1,
        "lineType": "MOBILE",
        "lineValue": "11"
    }],
    "externalObject": {

}}
  ResultObj63 (Response2)
{
  "resultCode": "OK",
"errorDescription": "",
"message": "",
"resultObj": {
    "userLinesList": [{
        "lineId": 1,
        "lineType": "MOBILE",
        "lineValue": "11"
    }],
    "externalObject": ""
},
"systemTime": 1536131455

 }
我在比较postman中的两个响应,但当我比较时,我无法进入userLinesList和externalObject内部,我得到了它

as3.2的值是:[对象对象]——6.3的值是:[对象对象对象]

所以我只想进入这个userlist和externalObject进行比较

代码如下:-

console.log("Entered into assertion check");
tests["3.2 response is"+pm.variables.get("ResultObj32")+ "--------" +"6.3 response is"+pm.variables.get("ResultObj63")] = true;
var x = JSON.parse(pm.variables.get("ResultObj32"));
var y = JSON.parse(pm.variables.get("ResultObj63"));
console.log("3.2 response object length is "+Object.keys(x).length);
console.log("6.3 response object length is "+Object.keys(y).length);



for (var i=0; i<Object.keys(x).length; i++)
{
    //console.log("Execution entered into first i loop");
    if (y.hasOwnProperty(Object.keys(x)[i]) && Object.keys(x)[i] !== "resultObj")
    {
        console.log("Object present:"+Object.keys(x)[i]);
        var key = Object.keys(x)[i];
        console.log("Key is:"+key);
        console.log("Corresponding value in 3.2 is :"+Object.values(x)[i]);
        console.log("Corresponding value in 6.3 is :"+y[key]);
        tests["Validation of element in response: "+Object.keys(x)[i]+ "----------" + "3.2 value is:  " + Object.values(x)[i] + ":" + "6.3 value is:   "+y[key] ]= Object.values(x)[i] === y[key];
    }
    else if (y.hasOwnProperty(Object.keys(x)[i]) && Object.keys(x)[i] === "resultObj")
    {

        for (var len=0; len<Object.keys(x.resultObj).length; len++)
        {
            console.log("Element that is getting verified is:"+Object.keys(x.resultObj)[len]);
            if (y[Object.keys(x)[i]].hasOwnProperty(Object.keys(x.resultObj)[len]))
            {
                 console.log("Object present:"+Object.keys(x.resultObj)[len]);
                 var key = Object.keys(x.resultObj)[len];
                 var y1 = y[Object.keys(x)[i]];
                 console.log("Key is:"+key);
                console.log("Key in 3.2 is:"+key);
                console.log("key in 6.3 is:"+Object.keys(y.resultObj)[len]);
                 console.log("Corresponding value in 6.3 is:"+y1[Object.keys(x.resultObj)[len]]);

                 if(Object.values(x.resultObj)[len] ==="" && y1[Object.keys(x.resultObj)[len]])
                 {
                 tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + "---" + "6.3 value is:   "+Object.values(y.resultObj)[len]]= true;
                 }
                 else
                 {
                     tests["Validation of element in resultObj is failed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + "---" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]= false;
                 }
            }
            else if(y[Object.keys(x)[i]].hasOwnProperty(Object.keys(x.resultObj)[len]))
            {
                console.log("Object present:"+Object.keys(x.resultObj)[len]);
                 var key = Object.keys(x.resultObj)[len];
                 var y1 = y[Object.keys(x)[i]];
                 console.log("Key in 3.2 is:"+key);
                 console.log("Corresponding value in 6.3 is:"+y1[Object.keys(x.resultObj)[len]]);

                 if(Object.values(x.resultObj)[len] === "" && y1[Object.keys(x.resultObj)[len]] === "")
                 {
                 tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + "---" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]=true;
                 }
                 else
                 {
                   tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]=true;   
                 }

            }
            else if(y[Object.keys(x)[i]].hasOwnProperty(Object.keys(x.resultObj)[len]))
            {
                console.log("Object present:"+Object.keys(x.resultObj)[len]);
                 var key = Object.keys(x.resultObj)[len];
                  var y1 = y[Object.keys(x)[i]];
                 console.log("Key is:"+key);
                 console.log("Corresponding value in 6.3 is"+y1[Object.keys(x.resultObj)[len]]);
                 if(Object.values(x.resultObj)[len] !=="" && y1[Object.keys(x.resultObj)[len]] ===null)
                 {
                 tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]= true;
                 }
                 else
                 {
                  tests["Validation of element in resultObj is failed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]= false;   
                 }
            }
            else
            {
                tests["Validation of element in resultObj is failed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y[key] ]= false;
            }
        }
    }
    else
    {
        console.log("Object not present:"+Object.keys(x)[i]);
        tests["Validation of element in response: "+Object.keys(x)[i]+ "----------" + "3.2 value is:  " + Object.values(x)[i] + ":" + "6.3 value is:   "+y[key] ]= false;
    }

}
console.log(“进入断言检查”);
测试[“3.2响应为”+pm.variables.get(“ResultObj32”)+“-----------”+“6.3响应为”+pm.variables.get(“ResultObj63”)]=true;
var x=JSON.parse(pm.variables.get(“ResultObj32”);
var y=JSON.parse(pm.variables.get(“ResultObj63”);
log(“3.2响应对象长度为”+object.keys(x.length));
console.log(“6.3响应对象长度为”+对象.键(y).长度);

对于(var i=0;i你能发布你的邮递员窗口的截图吗


通常,在Postman中,当返回带有嵌套JSON值的响应时,对象旁边会有一个下拉箭头,该箭头在单击时会展开以显示内容。

查看代码时,您似乎在连接字符串和JavaScript对象。发生这种情况时,您会得到“[对象]”。要打破这种情况,请[对象]“您有三个选项。您可以有选择地取消对该对象的引用。请尝试:

console.log("Corresponding value in 6.3 is:"+ y1[Object.keys(x.resultObj)[len]].lineId);
或者吐出整个物体。试试看

console.log("Corresponding value in 6.3 is:"+ JSON.stringify(y1[Object.keys(x.resultObj)[len]]));
或者把它放在一个抽屉里,试试看

var innerObject = y1[Object.keys(x.resultObj)[len]]);
for(var innerKey in innerObject)
{
    console.log("run your magic code", innerKey, innerObject[innerKey]);
}
这些方法的挑战在于,随着时间的推移,它们将导致高维护。考虑这种方法。

首先,让我们在每个对象中添加一些元素,这些元素不在其他对象中。其次,让我们更改某些对象的数据类型。请注意lineId。在一个对象中,它是字符串,而在另一个对象中,它是数字

var ResultObj32 = {
"errorDescription": "",
"message": "",
"resultCode": "OK",
"internal32":{},
"resultObj": {
    "userLinesList": [{
        "lineId": "1",
        "lineType": "MOBILE",
        "lineValue": "11",
        "notin63": true
    }],
    "externalObject": {

}}};
  var ResultObj63 =
{
  "resultCode": "OK",
"errorDescription": "",
"message": "",
"internal63":{},
"resultObj": {
    "userLinesList": [{
        "lineId": 1,
        "lineType": "MOBILE",
        "lineValue": "11",
        "notin32":true
    }],
    "externalObject": ""
},
"systemTime": 1536131455

 };



function objCompare(obj1, obj2, missing) {
    missing = (undefined == missing ? false : missing);
    Object.entries(obj1).forEach(([key, value]) => {
        if(undefined == obj2[key] || null == obj2[key]) {
            console.log(key, "is not in obj2");
            return;
        }
       if (value && ["string", "number", "boolean"].indexOf(typeof value) < 0) {
            objCompare(obj1[key], obj2[key], missing);
            return;
        }
        if(missing){
            return;
        }
        if(obj1[key] == obj2[key] && obj1[key] === obj2[key]) {
            console.log(key, value, "Is in both Objects and they are the same type");
        }
        if(obj1[key] == obj2[key] && obj1[key] !== obj2[key]) {
            console.log(key, value, "Is in both Objects and they are NOT the same type");
        }
    });
}
请注意,我在第二次调用中反转了对象,并且报告始终为obj2。然后我得到的是两个结果集,一个显示它们相同,另一个显示它们相同但类型不同

Comparing obj32 to obj63 as obj2
errorDescription  Is in both Objects and they are the same type
message  Is in both Objects and they are the same type 
resultCode OK Is in both Objects and they are the same type
internal32 is not in obj2
lineId 1 Is in both Objects and they are NOT the same type
lineType MOBILE Is in both Objects and they are the same type
lineValue 11 Is in both Objects and they are the same type
notin63 is not in obj2

Comparing ob63 to obj32 as obj2
internal63 is not in obj2
notin32 is not in obj2
systemTime is not in obj2

您是否创建了测试?是否正在进行比较。
object对象
似乎是一个解析问题。将其包装在
JSON.parse()
中可能会有所帮助。是的,但我也面临同样的问题:-(你能用测试代码更新问题吗?你是如何检查这两个问题的responses@DannyDainton我无法更新问题:-(呃…你能使用编辑按钮吗?很遗憾,我无法添加屏幕截图:-(你能看一看吗?我已经更新了问题,试着把你试图登录的对象放在JSON.stringify()中)。非常感谢您的回答,但使用它是我想要的最有效的方式。仅对于下面的响应,它不会检查externalObject、lineValue和systemtime。此外,如果在两个响应中有不匹配的内容,则它不会显示在控制台中,而不是测试响应失败-ResultToBj32(Response1)“errorDescription”:“message”:“resultCode”:“OK”,“resultObj”:{“userLinesList”:[{“lineId”:1,“lineType”:“MOBILE”,“lineValue”:“11”}],“externalObject”:{}}那么,您是否需要更改当前的if语句或添加更多的if语句来测试失败条件?不,我只是在两个响应中更改了一些内容,但没有进行比较
Comparing obj32 to obj63 as obj2
errorDescription  Is in both Objects and they are the same type
message  Is in both Objects and they are the same type 
resultCode OK Is in both Objects and they are the same type
internal32 is not in obj2
lineId 1 Is in both Objects and they are NOT the same type
lineType MOBILE Is in both Objects and they are the same type
lineValue 11 Is in both Objects and they are the same type
notin63 is not in obj2

Comparing ob63 to obj32 as obj2
internal63 is not in obj2
notin32 is not in obj2
systemTime is not in obj2