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

问题描述 投票:1回答:1

我试图从我的响应中获取一个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,但这不起作用。

请帮忙

dsl karate
1个回答
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'
© www.soinside.com 2019 - 2024. All rights reserved.