如何比较两个JSON响应中存在的字段并验证它?

发布于 2025-02-12 16:57:31 字数 1608 浏览 1 评论 0原文

我需要比较响应1中出现的所有国家 /地区也应在Response2中存在。 那么,我该如何从回答中夺取所有国家并验证呢? 我尝试在Arraylist中存储国家,但无法验证它。

JSON响应1:

{
  "plan": {
    "program": "gtr",
    "syter": "yes"
  },
  "Map": {
    "List": [
      {
        "id": "tyt6577",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "us",
        "triag": null
      },
      {
        "id": "yyaqtf6327",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "aus",
        "triag": null
      },
      {
        "id": "676hwjsgvhgv",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "rus",
        "triag": null
      },
      {
        "id": "676hsdhgv",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "spa",
        "triag": null
      },
      {
        "id": "623ujhhgv",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "cha",
        "triag": null
      }
    ]
  }
}

JSON响应2:

[
  {
    "id": "tyt6577",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "rus",
    "triag": null
  },
  {
    "id": "yyaqtf6327",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "spa",
    "triag": null
  },
  {
    "id": "676hwjsgvhgv",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "us",
    "triag": null
  },
  {
    "id": "676hsdhgv",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "aus",
    "triag": null
  },
  {
    "id": "623ujhhgv",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "cha",
    "triag": null
  }
]

I need to compare all the countries present in Response1 should be present in Response2 also.
So How Can I take all the countries from both the responses and validate?
I tried on storing country in ArrayList but not able to validate it.

Json Response 1:

{
  "plan": {
    "program": "gtr",
    "syter": "yes"
  },
  "Map": {
    "List": [
      {
        "id": "tyt6577",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "us",
        "triag": null
      },
      {
        "id": "yyaqtf6327",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "aus",
        "triag": null
      },
      {
        "id": "676hwjsgvhgv",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "rus",
        "triag": null
      },
      {
        "id": "676hsdhgv",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "spa",
        "triag": null
      },
      {
        "id": "623ujhhgv",
        "proxy": "ENABLED",
        "type": "BENEFIT",
        "country": "cha",
        "triag": null
      }
    ]
  }
}

Json Response 2:

[
  {
    "id": "tyt6577",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "rus",
    "triag": null
  },
  {
    "id": "yyaqtf6327",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "spa",
    "triag": null
  },
  {
    "id": "676hwjsgvhgv",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "us",
    "triag": null
  },
  {
    "id": "676hsdhgv",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "aus",
    "triag": null
  },
  {
    "id": "623ujhhgv",
    "proxy": "ENABLED",
    "type": "BENEFIT",
    "country": "cha",
    "triag": null
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2025-02-19 16:57:31

这对你有用

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
...
List<String> country = res.jsonPath().get("Map.List.country");
List<String> country2 = res2.jsonPath().get("country");
assertThat(country2, containsInAnyOrder(country.toArray()));

This would work for you

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
...
List<String> country = res.jsonPath().get("Map.List.country");
List<String> country2 = res2.jsonPath().get("country");
assertThat(country2, containsInAnyOrder(country.toArray()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文