首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在自动化脚本中比较两个JSON数据的更好方法

在自动化脚本中比较两个JSON数据的更好方法
EN

Stack Overflow用户
提问于 2018-01-05 08:37:39
回答 2查看 1.2K关注 0票数 2

我是JBehave Rest自动化脚本的新手。下面我编写了几行代码,其中我的要求是将每个实际的JSON字段数据与预期的数据进行比较。在这里,JSON中的字段数量是巨大的,我如何才能以最好的方式编写脚本来处理这种情况。

代码语言:javascript
复制
JSONObject actualjson = new JSONObject(actualJsonresponse);
JSONArray actualjsonData = actualjson.getJSONArray("outputDtlList");
JSONObject expectedjson = new JSONObject(RTRestServicesBean.getConfigurationJsonConfigValue());
JSONArray expectedjsonData = expectedjson.getJSONArray("outputDtlList");

String actual_storagetype = actualjsonData.getJSONObject(0).getString("storageType");
String expected_storagetype = expectedjsonData.getJSONObject(0).getString("storageType");

Assert.assertEquals(actual_storagetype, expected_storagetype);

String actual_locnNbr = actualjsonData.getJSONObject(0).getString("locnNbr");
String expected_locnNbr = expectedjsonData.getJSONObject(0).getString("locnNbr");

Assert.assertEquals(actual_locnNbr, expected_locnNbr);
EN

回答 2

Stack Overflow用户

发布于 2018-01-06 05:26:30

考虑使用net.javacrumbs.json-unit:json-unit

代码语言:javascript
复制
import static net.javacrumbs.jsonunit.JsonAssert.*;

...

assertJsonEquals(expectedjson, actualjsonData);
票数 1
EN

Stack Overflow用户

发布于 2018-01-06 09:23:30

考虑切换到Karate,它可以更好地处理完整的JSON有效负载比较:

代码语言:javascript
复制
* def json = { foo: 'world', hey: 'ho', zee: [1, 2, 3] }
* remove json.hey
* match json == { foo: 'world', zee: [1, 2, 3] }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48105630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档