获取返回的Json中内层的数据
1、Response body :Json value check代码格式如下:
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
“Your test name”可自定义
pm.response.json()响应体转换为json,并存储在jsonData中
jsonData.value获取json内容,比较字段的值
2、实例如下所示:
{
"status": 0,
"result": {
"location": {
"areacode": "101010100",
"name": "北京",
"country": "中国",
"path": "北京,北京市,北京市,中国"
},
"realtime": {
"text": "阴",
"code": "02",
"temp": 17.2,
"feels_like": 15,
"rh": 12,
"wind_class": "2级",
"wind_speed": 2.9,
"wind_dir": "东北风",
"wind_angle": 63,
"prec": 0.0,
"clouds": 95,
"vis": 30000,
"pressure": 1012,
"dew": -12,
"uv": 1,
"weight": 7,
"brief": "凉而干燥",
"detail": "天气干燥,注意肌肤保湿哦"
},
"last_update": "2023-04-20 17:16"
}
}
3、假如获取“areacode”的值进行断言,格式如下
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.result.location.areacode).to.eql("101010100");
});
4、结果如下

文章讲述了如何在Postman中处理和验证JSON响应数据。通过`pm.response.json()`将响应体转化为JSON对象,然后可以访问嵌套属性,如`jsonData.result.location.areacode`来获取特定值,并使用`pm.expect`进行断言。示例展示了如何对天气API的响应进行断言,特别是获取并检查`areacode`字段。
2万+

被折叠的 条评论
为什么被折叠?



