使用Postman

时间:2017-03-17 05:57:49

标签: javascript json google-chrome-app assert postman

我在POSTMAN chrome app中使用GET请求从API返回了嵌套的JSON。我的JSON看起来像这样

"result": [
{
  "_id": "some_id",
  "name": "India",
  "code": "IN",
  "link": "http://www.india.info/",
  "closingTime": "2017-02-25T01:12:17.860Z",
  "openingTime": "2017-02-25T06:12:17.205Z",
  "image": "image_link",
  "status": "online",
  "serverStatus": "online",
  "games": [
    {
      "_id": "some_game_id1",
      "name": "Cricket"
    },
    {
      "_id": "some_another_id1",
      "name": "Baseball"
    },
    {
      "_id": "some_another_id_2",
      "name": "Basketball"
    }
  ]
},
{
  "_id": "some_id",
  "name": "Australia",
  "code": "AUS",
  "link": "https://www.lonelyplanet.com/aus/adelaide",
  "closingTime": "2017-02-28T05:13:38.022Z",
  "openingTime": "2017-02-28T05:13:38.682Z",
  "image": "some_image_url",
  "status": "offline",
  "serverStatus": "online",
  "games": [
    {
      "_id": "some_game_id_2",
      "name": "Cricket"
    },
    {
      "_id": "some_another_id_3",
      "name": "Kho-Kho"
    },
    {
      "_id": "some_another_id_4",
      "name": "Badminton"
    },
    {
      "_id": "some_another_id_5",
      "name": "Tennis"
    }
  ]
},

我正在尝试测试我的回复正文是"name":"India""game" "some_game_id1"是否包含"name":"cricket"

我经历了这个link,其答案是为#34; name"创建一个数组,然后在数组中检查数组是否包含该值。我试过了,但我的代码失败了。

另外,我尝试使用这个 -

在JSON体内通过索引搜索元素
   var searchJSON = JSON.parse(responseBody);
   tests["name contains India"] = searchJSON.result.name[0]==="India";

但这也失败了。我尝试使用附加了上面第二行代码的.value,但它也失败了。我怎么检查这个东西?

1 个答案:

答案 0 :(得分:4)

您需要在[0](这是一个数组)之后放result而不是name(这是一个字符串)。

此外,使用正则表达式检查名称是否包含'India',因为使用===仅检查名称是否恰好是印度。

var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)

<小时/>

演示片段:

var responseBody = `{
  "result": [{
      "_id": "some_id",
      "name": "India",
      "code": "IN",
      "link": "http://www.india.info/",
      "closingTime": "2017-02-25T01:12:17.860Z",
      "openingTime": "2017-02-25T06:12:17.205Z",
      "image": "image_link",
      "status": "online",
      "serverStatus": "online",
      "games": [{
          "_id": "some_game_id1",
          "name": "Cricket"
        },
        {
          "_id": "some_another_id1",
          "name": "Baseball"
        },
        {
          "_id": "some_another_id_2",
          "name": "Basketball"
        }
      ]
    },
    {
      "_id": "some_id",
      "name": "Australia",
      "code": "AUS",
      "link": "https://www.lonelyplanet.com/aus/adelaide",
      "closingTime": "2017-02-28T05:13:38.022Z",
      "openingTime": "2017-02-28T05:13:38.682Z",
      "image": "some_image_url",
      "status": "offline",
      "serverStatus": "online",
      "games": [{
          "_id": "some_game_id_2",
          "name": "Cricket"
        },
        {
          "_id": "some_another_id_3",
          "name": "Kho-Kho"
        },
        {
          "_id": "some_another_id_4",
          "name": "Badminton"
        },
        {
          "_id": "some_another_id_5",
          "name": "Tennis"
        }
      ]
    }
  ]
}`

var tests = {}

var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)

console.log(tests) //=> { "name contains India": true }