如何从JSON文件中提取数据来编写测试用例

How to extract the data from JSON file to write test case

如何从 postman 的 JSON 输出中提取所需的 parent/child 节点值。

我需要从下面的 JSON 文件中提取 model.ConfirmPassword

{
  "Message": "The request is invalid.",
  "ModelState": {
  "model.ConfirmPassword": [
  "The password and confirmation password do not match."
  ]
}

要传递什么 属性 才能得到它。 jsonData.value 没有像我下面提到的那样工作。

var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.value;

Edit 在我错过之前 model.ConfirmPassword 嵌套在 ModelState 中。

这是怎么读的 属性:

var x = jsonData.ModelState['model.ConfirmPassword'];
console.log(x);

这是原话:http://plnkr.co/edit/rCtVijwWpPgX7SdnP2ja

答案应该是

var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.ModelState['model.ConfirmPassword'][0] ==="The password and confirmation password do not match.";

需要在 jsonData.ModelState['model.ConfirmPassword'][0] 中将索引作为 [0] 传递 否则,如果我们想要访问子元素,它将无法工作。 它可以是 [0] 或特定索引。