VSCode REST 客户端 - 如何从响应中的数组获取值

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

我正在尝试使用表达式从响应中获取值“141”

@second = {{getall.response.body.[1].id}}

HTTP/1.1 200 OK
Connection: close
Content-Type: application/json; charset=utf-8
Date: Thu, 07 Dec 2023 09:20:21 GMT
Server: Kestrel
Transfer-Encoding: chunked

[
  {
    "id": 140,
    "name": "Orlov 6 uger",
    "createdDate": "2023-12-07T09:01:29.0030238",
    "lastModifiedDate": "2023-12-07T09:07:31.4865664"
  },
  {
    "id": 141,
    "name": "Ferie 6 uger- cloud",
    "createdDate": "2023-12-07T09:11:57.7474033",
    "lastModifiedDate": "2023-12-07T09:11:57.7474044"
  }
]

我可以毫无问题地使用第一个“id”

# @name getfirst
GET https://{{baseUrl}}/api/todos/140

@first = {{getfirst.response.body.id}}
###
GET https://httpbin.org/get?id={{first}}

但是当我尝试使用以下命令访问数组中的 2 个项目时

@second = {{getall.response.body.[1].id}}
使用

# @name getall
GET https://{{baseUrl}}/api/todos

@second = {{getall.response.body.[1].id}}
###
GET https://httpbin.org/get?id={{second}}

变量未设置


{
  "args": {
    "id": "{{getall.response.body.[1].id}}"
  },

如何让@second获得值“141”?

vscode-extensions rest-client
1个回答
0
投票

答案是:

@second = {{getall.response.body.1.id}}

不是:

@second = {{getall.response.body.[1].id}}
© www.soinside.com 2019 - 2024. All rights reserved.