【问题标题】:How to iterate over a JSON array and log the attributes that have a certain value如何遍历 JSON 数组并记录具有特定值的属性
【发布时间】:2021-04-15 03:20:51
【问题描述】:

我有一个看起来像这样的响应正文

{
   "data_sets":[
    { 
           "author": {
               "name": "foo"
           },
           "category": "zip",
           "client": true,
           "data_type": "zip",
           "index": "completed",
           "id": 200,
           "params": {}
    },
    {
           "author": {
               "name": "foo2"
           },
           "category": "zip",
           "client": true,
           "data_type": "zip",
           "index": "completed",
           "id": 190,
           "params": {}
    }]}

我试图在我的邮递员测试中迭代我的数千个数据集,并且只是简单地 console.log 等于我的环境变量中的 id 的 id。对于这种情况,假设是 190。我正在尝试这样的事情,但它不起作用。它没有失败,但没有给我预期的输出。

这是我在邮递员中的代码

    pm.test("the response has the right id", () => {
    id_dataset = pm.environment.get("id");
    var jsonData = pm.response.json();
    for(var i = 0; i < jsonData.data_sets.length; i++)
   if(jsonData.data_sets[i].id == id_dataset)
     console.log(jsonData.data_sets[i].id);
    });

有人可以帮忙吗?

【问题讨论】:

  • i &lt; jsonData.data_sets.length(注意也可以用const data_set = jsonData.data_sets.find(item =&gt; item.id === id_dataset);替换循环)
  • (另外说明:服务器响应是 JSON 格式的文本。pm.response.json(); 解析该 JSON 文本,生成一个对象。该对象又包含 .data_sets,它是一个数组. 没有“JSON 数组”或“JSON 对象”之类的东西。)
  • JSON 无效。它缺少一些逗号,第二个元素缺少}
  • @Shultz 请使用数组方法find() 检查我的解决方案,并告诉我这是否适合您。
  • 我在我的解决方案中添加了可选链接 ?.id,以防止在 ID 不存在时引发错误。

标签: javascript arrays json postman postman-pre-request-script


【解决方案1】:

你忘记了 .length 在你的 for

  const jsonData = {data_sets: [ { "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": false, "data_type": "zip", "index": "completed", "id": 200, "params": {} }, { "author": { "name": "foo2" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 190, "params": {} }]};

 const id_dataset = 200;
 for(var i = 0; i < jsonData.data_sets.length; i++)
   if(jsonData.data_sets[i].id == id_dataset)
     console.log(jsonData.data_sets[i].id);
  

你也可以使用map

const jsonData = {data_sets: [ { "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} },{ "author": { "name": "foo" }, "category": "zip", "client": false, "data_type": "zip", "index": "completed", "id": 200, "params": {} }, { "author": { "name": "foo2" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 190, "params": {} }]};

const id_dataset = 200;

 jsonData.data_sets.map(item=>item.id==id_dataset && console.log(item.id))

【讨论】:

  • 如果您不使用结果,请使用 forEach 而不是 map
  • 我更改了代码以进行这些更新,但日志中仍然没有输出
  • 如果你想查看console.log只是为了测试,最好少写代码
  • @Shultz 你能提供console.log(jsonData) 的结果吗?可能您需要使用 .json() 解析响应
  • 我的最终目标是断言 190 的 id 有一个“客户”:true,但这不起作用,所以我将一步一步弄清楚每个部分
【解决方案2】:

我们应该可以使用find()快速搜索所有记录,找到匹配id为190的记录。试试这个:

pm.test("the response has the right id", () => {
    id_dataset = pm.environment.get("id"); // 190
    var jsonData = pm.response.json();
    console.log(jsonData.data_sets.find(set => set.id === id_dataset)?.id);
});

这里是原版 JS 中的相同逻辑:

let id_dataset = 190;

let data_sets = [ { "author": { "name": "foo" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 200, "params": {} }, { "author": { "name": "foo2" }, "category": "zip", "client": true, "data_type": "zip", "index": "completed", "id": 190, "params": {} }];

console.log(data_sets.find(set => set.id === id_dataset)?.id);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 2014-10-22
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
相关资源
最近更新 更多