如何从空手道数组格式的json响应中获取值

时间:2018-09-03 06:19:21

标签: dsl karate

我正在尝试从响应中获取一个JSONArray格式的值。我想从第一个数组对象中获取ID。

{
  "count": 1,
  "next": "",
  "previous": "",
  "list": [
    {
      "id": "f78bde61-056a-4358-bbac-50bf524c965c",
      "name": "Auto_Approval",
      "description": "Lambda Functions Demo",
      "context": "hjk",
      "version": "v1",
      "provider": "hk",
      "status": "PUBLISHED",
      "thumbnailUri": null
    },
    {
      "id": "f78bde61-056a-4358-bbac-50bf524c965c",
      "name": "Auto_Approval",
      "description": "Lambda Functions Demo",
      "context": "knkl",
      "version": "v1",
      "provider": "uygi",
      "status": "PUBLISHED",
      "thumbnailUri": null
    }
  ],
  "pagination": {
    "total": 1,
    "offset": 0,
    "limit": 25
  }
}

我想获取id的值。

我尝试了response.list[0].id,但这不起作用。

请帮助

1 个答案:

答案 0 :(得分:0)

您的JSON错误,缺少逗号。以后请多加小心。

粘贴以下内容,然后查看其效果。

* def response = 
"""
{
   "count":1,
   "next":"",
   "previous":"",
   "list":[
      {
         "id":"f78bde61-056a-4358-bbac-50bf524c965c",
         "name":"Auto_Approval",
         "description":"Lambda Functions Demo",
         "context":"hjk",
         "version":"v1",
         "provider":"hk",
         "status":"PUBLISHED",
         "thumbnailUri":null
      },
      {
         "id":"f78bde61-056a-4358-bbac-50bf524c965c",
         "name":"Auto_Approval",
         "description":"Lambda Functions Demo",
         "context":"knkl",
         "version":"v1",
         "provider":"uygi",
         "status":"PUBLISHED",
         "thumbnailUri":null
      }
   ],
   "pagination":{
      "total":1,
      "offset":0,
      "limit":25
   }
}
"""
* def id = response.list[0].id
* match id == 'f78bde61-056a-4358-bbac-50bf524c965c'