为了账号安全,请及时绑定邮箱和手机立即绑定

什么是正确的 JsonPath get() 格式,用于选择 JSON 对象数组中的字段值

什么是正确的 JsonPath get() 格式,用于选择 JSON 对象数组中的字段值

达令说 2022-07-06 17:03:24
我正在放心地进行 API 发布调用并在正文中获得响应。然后,我需要获取此响应并选择特定的字段值并将它们存储为字符串,以便稍后与其他字符串对象进行比较。我编写了 jsonpath 来获取顶级字段值(如 id、状态、类型、国家等),但是当我必须进入返回的 json 数组中的一个对象时,我无法正确获取格式get() 方法。以下是返回的 Json 示例:{  "id": "ABC123",  "status": "NEW",  "type": "PERSONAL",  "country": "United States",  "totalBalances": {},  "availableBalances": {},  "fields": [    {      "fieldType": "mobilephone",      "value": "14216904425",      "fieldId": "personalMobileNumber"    },    {      "fieldType": "email",      "value": "user12345@work.com",      "fieldId": "personalEmail"    },    {      "fieldType": "STRING",      "value": "John Doe",      "fieldId": "individualName"    }  ]}这是我试图格式化以适应 get() 方法的 json 路径,但每次我都得到一个非法参数异常(java.lang.IllegalArgumentException:无效的 JSON 表达式)我试图让它工作。基本上我需要在数组中识别正确的对象并获取正确的字段值。在这种情况下,这是 fieldId 字段,我想要字段“值”值(John Doe),以便我可以将其保存到 String 对象:JsonPath pathToAccountName = response.jsonPath();String accountName = pathToAccountName.get("fields[?(@.fieldId=='individualName')].value")我使用https://jsonpath.curiousconcept.com/来获取 VALID json 路径:$.fields[?(@.fieldId=='individualName')].value但是我尝试了一切将其转换为 get() 方法可以接受的东西,但没有运气。搜索这里的所有帖子和放心的技术文档也没有帮助。
查看完整描述

2 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

Rest Assured 使用 Groovy 的 Gpath。因此,您的查询可能如下所示:


JsonPath pathToAccountName = response.jsonPath();

String value = jsonPath.getString("fields.find { it.fieldId == 'individualName' }.value");

在这里您可以找到一些示例(它是关于处理 XML,但也适用于 JSON): http: //groovy-lang.org/processing-xml.html


查看完整回答
反对 回复 2022-07-06
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

我需要知道如何获得这个输出 -

JSON 文档 -{ "status": "E000", "customerId": "VjAxI2VhNzg5ZmJlLWIyNjAtNGZlOS1iZDNkLTdjMmU1MjA2ZmVhZA", "merchantId": "1", "cards": [ { "cardType": "DEBIT", "cardIssuer": "AXIS", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "HDFC Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "VISA", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "DEBIT", "cardIssuer": "Punjab National Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "MASTERCARD", "nickName": null, "expired": null, "cardMigrationStatus": "DONE", "tur": null, "category": null } ], "status_mssg": null }

我想要这个输出 - [ { "cardType": "DEBIT", "cardIssuer": "AXIS", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "HDFC Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "DEBIT", "cardIssuer": "Punjab National Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null } ]

我正在使用这个 - jsonPathValidator.getString("$.cards[?(@.cardMigrationStatus == 'OPEN' && @.cardBrand == 'MASTERCARD')]");

但没有得到输出。


查看完整回答
反对 回复 2022-07-06
  • 2 回答
  • 0 关注
  • 127 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信