如何使用邮递员在 JSON 个对象数组中搜索特定对象

How do I search for a particular object within an array of JSON objects using postman

如何从 JSON 数组中的 JSON 对象中查询特定的 BusStopCode

 "value": [
        {
            "BusStopCode": "01012",
            "RoadName": "Victoria St",
            "Description": "Hotel Grand Pacific",
            "Latitude": 1.29684825487647,
            "Longitude": 103.85253591654006
        },
        {
            "BusStopCode": "01013",
            "RoadName": "Victoria St",
            "Description": "St. Joseph's Ch",
            "Latitude": 1.29770970610083,
            "Longitude": 103.8532247463225
        },
       

例如,如果我只想查找第一个对象,那么我要查询的公交车站代码是 01012

我当前的 URL 查询请求如下所示- http:///transport/dataservice/BusStops?BusStopCode=01012

这里 http://transport/dataservice/BusStops 是我的 URL

?BusStopCode=01012是我的路径

tl;dr: 你不能,除非他们在服务器端实现它。

Postman只是客户端。

当您发送 URL 时 - 服务器读取它并针对此特定的 url / url + 参数在我们的例子中进行实现。

如果服务器有类似 http://transport/dataservice/BusStops?BusStopCode=01012 的实现,他们应该将其公开给您。你猜不到他们的 API.

是什么

您可以在 Postman 中使用 JSONpath Visualizer 过滤您的响应数据。

要启用可视化工具,请将以下代码粘贴到创建请求的 Postman 的测试部分。

let template = `
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/jsonpath@1.0.2/jsonpath.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
</head>
<body>
    <div>
      <div>
        <input id="filter" style="width:450px;" type="text" placeholder="Example query: $..name.first">
      </div>
      <div>
        <button id="resetButton" style="background-color:red;color:white;">Reset</button>
        <input id="showErrors" type="checkbox" value="1"/>
        <span class="item-text" style="font-family:courier;">Show Evaluation Errors</span>
      </div>
      <div id="errors" style="font-family:courier;color:red;display:none;"></div>
      <div>
        <p id="content" style="font-family:courier;color:green;font-size:18px;"></p>
      </div>
    </div>
</body>
</html>

<script>
pm.getData( (error, value) => { 
    const extractedData = jsonpath.query(value, '$');
    
    $(function() {
        $('#filter').keyup(function() {
            try {
                let filteredData = jsonpath.query(extractedData, $(this).val());
                $("#content, #errors").empty();
                $("#content").append("<pre><code>" + JSON.stringify(filteredData, null, 4) + "</code></pre>");
            } catch (err) {
                console.info(err);
                $("#errors").empty();
                $("#errors").append("<pre><code>" + err + "</code></pre>");
            }
        });
    });
    
    $( "#resetButton" ).click(function() {
        $("#content, #errors").empty();
        $("#filter").val('');
        $("#content").append("<pre><code>" + JSON.stringify(extractedData, null, 4) + "</code></pre>");
    })
})

$(function() {
  $("#showErrors").on("click",function() {
    $("#errors").toggle(this.checked);
  });
});
</script>`

pm.visualizer.set(template, pm.response.json())

这是例子。

还提供了示例数据以便更好地理解。