使用POSTMAN给出错误的Count的JSON测试

时间:2018-05-25 11:07:02

标签: json postman

我需要计算1个数组中的no.of.objects。在代码下方使用,但它给出了错误。

预期结果= 计数= 14

错误是 - 评估测试脚本时出错:JSONError:1:2处的意外标记“o”[object Object] ^

我无法理解错误。 Plz帮助我测试正确的代码。

对解析的回应 -

"fdArea":
[{
        "dAreaId": 2,
        "dArea": "Belgium",
        "dPrefixCode": "BE"
    },
    {
        "dAreaId": 3,
        "dArea": " Czech",
        "dPrefixCode": "CZ"
    },
    {
        "dAreaId": 6,
        "dArea": "France",
        "dPrefixCode": "FR"
    },
    {
        "dAreaId": 4,
        "dArea": " Germany",
        "dPrefixCode": "DE"
    },
    {
        "dAreaId": 7,
        "dArea": " Hungary",
        "dPrefixCode": "HU"
    },
    {
        "dAreaId": 8,
        "dArea": "Italy",
        "dPrefixCode": "IT"
    },
    {
        "dAreaId": 9,
        "dArea": "Netherlands",
        "dPrefixCode": "NL"
    },
    {
        "dAreaId": 10,
        "dArea": " Poland",
        "dPrefixCode": "PL"
    },
    {
        "dAreaId": 12,
        "dArea": "Slovakia",
        "dPrefixCode": "SK"
    },
    {
        "dAreaId": 14,
        "dArea": "South Africa",
        "dPrefixCode": "ZAF"
    },
    {
        "dAreaId": 5,
        "dArea": "Spain",
        "dPrefixCode": "ES"
    },
    {
        "dAreaId": 11,
        "dArea": "Sweden",
        "dPrefixCode": "SE"
    },
    {
        "dAreaId": 1,
        "dArea": " United Arab Emirates",
        "dPrefixCode": "ARE"
    },
    {
        "dAreaId": 13,
        "dArea": "United Kingdom",
        "dPrefixCode": "UK"
    }]

代码:

var responseJson = JSON.parse();
var count = responseJson.value.length;
console.log(count);

1 个答案:

答案 0 :(得分:0)

如果要检查fdArea数组中有多少个对象,可以使用_.each()选项卡中的Lodash Tests函数,将数字注销到控制台:< / p>

_.each(pm.response.json(), (arrItem) => {
        console.log(arrItem.fdArea.length)
})

使用与此类似的response数据集:

[
   {
      "fdArea":[
         {
            "dAreaId":2,
            "dArea":"Belgium",
            "dPrefixCode":"BE"
         },
         {
            "dAreaId":3,
            "dArea":" Czech",
            "dPrefixCode":"CZ"
         },
         {
            "dAreaId":6,
            "dArea":"France",
            "dPrefixCode":"FR"
         },
         {
            "dAreaId":4,
            "dArea":" Germany",
            "dPrefixCode":"DE"
         },
         {
            "dAreaId":7,
            "dArea":" Hungary",
            "dPrefixCode":"HU"
         },
         {
            "dAreaId":8,
            "dArea":"Italy",
            "dPrefixCode":"IT"
         },
         {
            "dAreaId":9,
            "dArea":"Netherlands",
            "dPrefixCode":"NL"
         },
         {
            "dAreaId":10,
            "dArea":" Poland",
            "dPrefixCode":"PL"
         },
         {
            "dAreaId":12,
            "dArea":"Slovakia",
            "dPrefixCode":"SK"
         },
         {
            "dAreaId":14,
            "dArea":"South Africa",
            "dPrefixCode":"ZAF"
         },
         {
            "dAreaId":5,
            "dArea":"Spain",
            "dPrefixCode":"ES"
         },
         {
            "dAreaId":11,
            "dArea":"Sweden",
            "dPrefixCode":"SE"
         },
         {
            "dAreaId":1,
            "dArea":" United Arab Emirates",
            "dPrefixCode":"ARE"
         },
         {
            "dAreaId":13,
            "dArea":"United Kingdom",
            "dPrefixCode":"UK"
         }
      ]
   }
]

它将返回Postman Console中的数字14