什么是 Rest Assured 正确的 JsonPath get() 格式,用于选择 JSON 对象数组中的字段值
What is the Rest Assured proper JsonPath get() format for selecting the value of a field within an array of JSON objects
我正在使用放心进行 API post 调用并在体内得到响应。然后我需要获取此响应和 select 特定字段值并将它们存储为字符串,以便稍后与其他字符串对象进行比较。我写 jsonpath 只是为了获取顶级字段值(如 id、status、type、country 等)但是当我必须进入 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"
}
]
}
这是 json 路径,我试图格式化以适应 get() 方法,但我每次都会收到非法参数异常(java.lang.IllegalArgumentException:无效的 JSON 表达式) 我试着让它工作。基本上我需要在数组中识别正确的对象并获取正确的字段值。在这种情况下,它是 fieldId 字段,我想要字段 "value" 值 (John Doe),以便我可以将其保存到 String 对象:
JsonPath pathToAccountName = response.jsonPath();
String accountName = pathToAccountName.get("fields[?(@.fieldId=='individualName')].value")
我使用 https://jsonpath.curiousconcept.com/ 获取有效 json 路径:
$.fields[?(@.fieldId=='individualName')].value
但我尝试了所有方法将其转换为 get() 方法可以接受的内容,但没有成功。在这里搜索所有 post 和放心的技术文档也没有帮助。
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
我需要知道如何得到这个输出 -
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')]");
但没有得到输出。
我正在使用放心进行 API post 调用并在体内得到响应。然后我需要获取此响应和 select 特定字段值并将它们存储为字符串,以便稍后与其他字符串对象进行比较。我写 jsonpath 只是为了获取顶级字段值(如 id、status、type、country 等)但是当我必须进入 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"
}
]
}
这是 json 路径,我试图格式化以适应 get() 方法,但我每次都会收到非法参数异常(java.lang.IllegalArgumentException:无效的 JSON 表达式) 我试着让它工作。基本上我需要在数组中识别正确的对象并获取正确的字段值。在这种情况下,它是 fieldId 字段,我想要字段 "value" 值 (John Doe),以便我可以将其保存到 String 对象:
JsonPath pathToAccountName = response.jsonPath();
String accountName = pathToAccountName.get("fields[?(@.fieldId=='individualName')].value")
我使用 https://jsonpath.curiousconcept.com/ 获取有效 json 路径:
$.fields[?(@.fieldId=='individualName')].value
但我尝试了所有方法将其转换为 get() 方法可以接受的内容,但没有成功。在这里搜索所有 post 和放心的技术文档也没有帮助。
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
我需要知道如何得到这个输出 -
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')]");
但没有得到输出。