[英]Using rest-assured get value using path method
I want to extract value of sdurl
from the below JSON. 我想从下面的JSON中提取
sdurl
值。
I am using the below satement as shown using System.out.println but I dont get the result. 我正在使用下面的状态,如System.out.println所示,但没有得到结果。 How do I do it when the field itself contains "."
当字段本身包含“。”时,我该怎么办?
JSON JSON格式
{
"data":{
"H1":{
"com.abc.def":{
"a_enabled":false,
"b_config":true
},
"c.s.urls":{
"s.d.url":"https://url1.com",
"w.p.url":"https://url2.com",
"s.c.url":"https://url3.com"
},
"com.abc.con":{
"e_n":true,
"a_r":false,
"c_t":"XYZMB"
}
},
"dCId":"ABCD"
}
}
ExtractableResponse<Response> spec = given()
.request().log().all()
.expect().statusCode(200)
.when()
.get(EndpointsCloudServices.getConfigUrl() + "?" + params)
.then().log().body()
.extract();
//want to get value of s.d.url
System.out.println("Triage???? " + spec.path("data.H1.c.s.urls.s.d.url"));
Give a try for the following, it will return value of sdurl
(pay attention for square brackets and single quotes): 尝试以下操作,它将返回
sdurl
值(注意方括号和单引号):
spec.path("data.H1.['csurls'].['sdurl']")
or even shorter, if you're sure that sdurl
is an unique name across the whole json document: 甚至更短,如果您确定
sdurl
是整个json文档中的唯一名称:
spec.path("$.['sdurl']")
And next, this is just to illustrate common case of referring field which contains dots in its name by using JSONPath expression - all you need is to wrap the field name in ['
and ']
接下来,这只是为了说明使用JSONPath表达式引用字段名称中包含点的常见情况-您所需要做的就是将字段名称包装在
['
和']
Example JSON: JSON示例:
{
"field.name": "value",
"nested": {
"field.with.dot": {
"field.inside": "anotherValue"
}
}
}
Example valid JSONPath expressions to access corresponding field values: 有效的JSONPath表达式示例,用于访问相应的字段值:
$['field.name']
$.['field.inside']
nested.['field.with.dot'].['field.inside']
Hint: you can quickly test your JSONPaths agains your json using tools like online evaluator or expression tester 提示:您可以使用在线评估器或表达式测试器之类的工具快速测试JSONPaths和json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.