当多个值具有相同名称时,将值存储为 Postman 中的变量

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

我目前正在开发一个项目,涉及使用测试脚本将即将到来的公交车到达时间存储在变量中。当我运行 API 请求时,我得到以下结果:

{
    "route_departures": [
        {
            "global_route_id": "TSL:1900",
            "itineraries": [
                {
                    "branch_code": "",
                    "direction_headsign": "*******",
                    "direction_id": 0,
                    "headsign": "*******"",
                    "merged_headsign": "*******"",
                    "schedule_items": [
                        {
                            "departure_time": 1709116320,
                            "is_cancelled": false,
                            "is_real_time": false,
                            "rt_trip_id": "13714672",
                            "scheduled_departure_time": 1709116320,
                            "trip_search_key": "TSL:44646441:935:4:0",
                            "wheelchair_accessible": 1
                        },
                        {
                            "departure_time": 1709116980,
                            "is_cancelled": false,
                            "is_real_time": false,
                            "rt_trip_id": "13714671",
                            "scheduled_departure_time": 1709116980,
                            "trip_search_key": "TSL:44646441:934:4:0",
                            "wheelchair_accessible": 1
                        },
                        {
                            "departure_time": 1709118780,
                            "is_cancelled": false,
                            "is_real_time": false,
                            "rt_trip_id": "13714665",
                            "scheduled_departure_time": 1709118780,
                            "trip_search_key": "TSL:44646441:934:4:1",
                            "wheelchair_accessible": 1
                        }
                    ]
                }
            ],
            "mode_name": "Bus",
            "real_time_route_id": "6841",
            "route_color": "1e4f75",
            "route_long_name": "*******"",
            "route_network_id": "*******"",
            "route_network_name": "*******"",
            "route_short_name": "N9",
            "route_text_color": "ffffff",
            "route_type": 3,
            "sorting_key": "N9",
            "tts_long_name": "*******"",
            "tts_short_name": "N9"
        },
        {
            "global_route_id": "TSL:1894",
            "itineraries": [
                {
                    "branch_code": "",
                    "direction_headsign": "*******"",
                    "direction_id": 0,
                    "headsign": "*******"",
                    "merged_headsign": "*******"",
                    "schedule_items": [
                        {
                            "departure_time": 1709056473,
                            "is_cancelled": false,
                            "is_real_time": true,
                            "rt_trip_id": "13681045",
                            "scheduled_departure_time": 1709056080,
                            "trip_search_key": "TSL:44646441:317:3:17",
                            "wheelchair_accessible": 1
                        },
                        {
                            "departure_time": 1709057220,
                            "is_cancelled": false,
                            "is_real_time": true,
                            "rt_trip_id": "13681044",
                            "scheduled_departure_time": 1709057040,
                            "trip_search_key": "TSL:44646441:317:3:18",
                            "wheelchair_accessible": 1
                        },
                        {
                            "departure_time": 1709058031,
                            "is_cancelled": false,
                            "is_real_time": true,
                            "rt_trip_id": "13681043",
                            "scheduled_departure_time": 1709057940,
                            "trip_search_key": "TSL:44646441:317:3:19",
                            "wheelchair_accessible": 1
                        }
                    ]
                }
            ],
            "mode_name": "Bus",
            "real_time_route_id": "6667",
            "route_color": "2a61a0",
            "route_long_name": "*******",
            "route_network_id": "T*******"",
            "route_network_name": "*******"",
            "route_short_name": "160",
            "route_text_color": "ffffff",
            "route_type": 3,
            "sorting_key": "160",
            "tts_long_name": "*******"",
            "tts_short_name": "1 60"
        }
    ]
}

我正在尝试使用测试脚本将“TSL 1900”路线最快出发的

departure_time
值存储在变量中。

我是邮递员的初学者,一般使用 API,对 JSON 的了解非常有限,所以我的解决方案很可能在这种情况下不起作用。

请注意,出于个人隐私考虑,我已审查所有地理信息。

我使用了这个测试脚本,但它根本没有给出预期的结果。

console.log(pm.response.json())
const route_departures = pm.response.json().route_departures
const itineraries = route_departures.itineraries
const schedule_items = itineraries.schedule_items
let upcoming_departure = schedule_items.departure_time

这就是它产生的结果。

This is the result that comes from this test script

我也尝试过使用这个测试脚本:

console.log(pm.response.json())
const route_departures = pm.response.json().route_departures
const itineraries = route_departures.itineraries
pm.environment.set("upcoming_arrival", itineraries.schedule_items[0].departure_time);

并收到此错误:

this is the error that I received

json postman
1个回答
0
投票

您需要先了解 JSON 语法

JSON背景

#1 JSON 中的对象包含在大括号 {} 内。它由键值对组成,其中键必须是用双引号 "",

括起来的字符串

“key”:值(数字或布尔值没有引号,字符串用双引号“”括起来)

{
  "name": "John",
  "age": 21,
  "city": "New York"
}

#2

Array
JSON 中的数组包含在方括号 [] 中。

[
  "apple",
  "banana",
  "orange"
]

要访问第一个元素“apple”,您可以使用索引 [0],如下所示:

array[0]
-> “苹果”

array[1]
-> “香蕉”

array[2]
->“橙色”

所以 以 {} 开头:这表明 JSON 代表一个对象。大括号内的内容将由键值对组成。

以[]开头:表示该JSON代表一个数组。方括号内的内容将由值组成,可以是任何 JSON 数据类型。

邮递员背景

#1 json数据 jsonData 指 API 响应返回的 JSON 数据。

const jsonData = pm.response.json();

#2 console.log():console.log() 是一个 JavaScript 函数,用于将消息记录到控制台。

#3 数组访问

jsonData.route_departures[0]

jsonData.route_departures[0] 指访问 jsonData 对象中的route_departures 数组的第一个元素(索引0)。

轻松调试打印值

console.log()

控制台(Postman 中的左下方)

查找 TSL 最快出发时间:1900

Tests
选项卡

const jsonData = pm.response.json();

// Find the entry for the "TSL 1900" route
var tsl1900Route = jsonData.route_departures.find(route => route.global_route_id === "TSL:1900");

// If the route is found, find the soonest departure time
if (tsl1900Route) {
    const soonestDepartureTimeUnix = Math.min(...tsl1900Route.itineraries[0].schedule_items.map(item => item.departure_time));
    const soonestDepartureTime = new Date(soonestDepartureTimeUnix * 1000).toLocaleString();
    console.log("Soonest Departure Time:", soonestDepartureTimeUnix);
    console.log("Soonest Departure Time (Readable):", soonestDepartureTime);
} else {
    console.log("TSL 1900 route not found");

控制台结果

Soonest Departure Time:
1709116320
 
Soonest Departure Time (Readable):
2/28/2024, 5:32:00 AM

© www.soinside.com 2019 - 2024. All rights reserved.