Parameters 从postman中的响应检索字符串时评估测试脚本时出错

Parameters 从postman中的响应检索字符串时评估测试脚本时出错,parameters,postman,Parameters,Postman,从下面的响应结构中,我想检索id字段并将其保存在postman中的环境变量中 我尝试了所有可能的方法来做这件事,但是做不到 { "childMaxAge": 13, "selectedForBooking": [ { "id": "0661be61-d0ae-411009-a96d-64229a", "firstName": "Test", "gender": {

从下面的响应结构中,我想检索id字段并将其保存在postman中的环境变量中

我尝试了所有可能的方法来做这件事,但是做不到

{
    "childMaxAge": 13,
    "selectedForBooking": [
        {
            "id": "0661be61-d0ae-411009-a96d-64229a",
            "firstName": "Test",
            "gender": {
                "code": "MALE",
                "name": "Male"
            }
   ]
}
我试过下面的方法,但也不起作用

tests["Status code is 201"] = responseCode.code === 201;
var data = JSON.parse(responseBody);
console.log(data);
var custid=JSON.parse(data.selectedForBooking[0].id);
tests ["selectedForBooking"]= postman.setEnvironmentVariable("CustId1", custid);
但它给出了错误:


评估测试脚本时出错:JSONError:1:2 0661be61-d0ae-411009-a96d-64229a处的意外标记“6”^

无需再次分析响应数据。 此外,如果您不打算断言任何内容,则无需使用
tests[]

tests["Status code is 201"] = responseCode.code === 201;
var data = JSON.parse(responseBody);
console.log(data);
var custid=data.selectedForBooking[0].id;
postman.setEnvironmentVariable("CustId1", custid);
var custid=JSON.parse(data.selectedForBooking[0].id)
试图用非json格式字符串的
0661be61-d0ae-411009-a96d-64229a创建json对象。

我曾经也有过类似的问题,我一直在努力

body.entries[0].attributes.device.type
我通过这样做获得了价值

body.entries[0].attributes['device.type']
确保使用单引号(我不知道为什么…)

希望这有帮助


Alexandre

您的JSON数据本身是错误的,这就是它显示错误的原因

它必须是:

{
    "childMaxAge": 13,
    "selectedForBooking": [
        {
            "id": "0661be61-d0ae-411009-a96d-64229a",
            "firstName": "Test",
            "gender": {
                "code": "MALE",
                "name": "Male"
            }
        }
   ]
}
现在你可以试试这个了

var custid=data.selectedForBooking[0].id


它将返回所需的值

Hi,我尝试了您的建议,但也给出了错误“评估测试脚本时出错:TypeError:无法读取未定义的属性“id”。您的错误表明“selectedForBooking”下的数组为空,您是否检查了响应是否实际返回?你的服务是公开的吗?我可以向它发出请求吗?嗨,我尝试了你的建议,但也给出了错误“评估测试脚本时出错:TypeError:无法读取未定义的属性'id'”嗯,这与我的情况不完全相同,你在测试前试过控制台.log(custid)吗?出现此错误时,selectedForBooking[0]似乎无法识别。。。您是否也可以在控制台中查看选择预订[0]???为什么要解析已经是json解析结果的json.parse数据?