英文:
How to fetch value from api response using rest Assured
问题
{
"data": {
"content": [
{
"absolute": 1800091.5124980211,
"percentage": 99.9,
"count": 79,
"currency": "USD",
"element": {
"merchantBrandName": "Bliz",
"merchantLogo": "png"
}
},
{
"absolute": 2170.220006465912,
"percentage": 0.1,
"count": 141,
"currency": "USD",
"element": {
"merchantBrandName": "Netflix",
"merchantLogo": "png1"
}
},
{
"absolute": 30.240000128746033,
"percentage": 0.0,
"count": 27,
"currency": "USD",
"element": {
"merchantBrandName": "Wal",
"merchantLogo": "svg"
}
},
{
"absolute": 22.59999990463257,
"percentage": 0.0,
"count": 20,
"currency": "USD",
"element": {
"merchantBrandName": "MAL",
"merchantLogo": "dining.svg"
}
},
{
"absolute": 19.800000429153442,
"percentage": 0.0,
"count": 18,
"currency": "USD",
"element": {
"merchantBrandName": "Ub",
"merchantLogo": "9XrD.png"
}
},
{
"absolute": 15.680000066757202,
"percentage": 0.0,
"count": 14,
"currency": "USD",
"element": {
"merchantBrandName": "way",
"merchantLogo": "iZ.png"
}
}
],
"page": {
"size": 6,
"totalElements": 6,
"totalPages": 1,
"number": 1
}
}
}
英文:
I am new to rest assured. I have a response json as below. where I have multiple nested json in an array having details of spends on particular Merchant. I need to get the spend value of Netflix only. How can I check for the condition where it is Netflix get the absolute value for that only ?
{
"data": {
"content": [
{
"absolute": 1800091.5124980211,
"percentage": 99.9,
"count": 79,
"currency": "USD",
"element": {
"merchantBrandName": "Bliz",
"merchantLogo": "png"
}
},
{
"absolute": 2170.220006465912,
"percentage": 0.1,
"count": 141,
"currency": "USD",
"element": {
"merchantBrandName": "Netflix",
"merchantLogo": "png1"
}
},
{
"absolute": 30.240000128746033,
"percentage": 0.0,
"count": 27,
"currency": "USD",
"element": {
"merchantBrandName": "Wal",
"merchantLogo": "svg"
}
},
{
"absolute": 22.59999990463257,
"percentage": 0.0,
"count": 20,
"currency": "USD",
"element": {
"merchantBrandName": "MAL",
"merchantLogo": "dining.svg"
}
},
{
"absolute": 19.800000429153442,
"percentage": 0.0,
"count": 18,
"currency": "USD",
"element": {
"merchantBrandName": "Ub",
"merchantLogo": "9XrD.png"
}
},
{
"absolute": 15.680000066757202,
"percentage": 0.0,
"count": 14,
"currency": "USD",
"element": {
"merchantBrandName": "way",
"merchantLogo": "iZ.png"
}
}
],
"page": {
"size": 6,
"totalElements": 6,
"totalPages": 1,
"number": 1
}
}
}
JsonPath js = new JsonPath(response);
js.get(key).toString();
I used to get value directly using JsonPath but now it is a condition based value I need to fetch.
Here I need to fetch the value of key "absolute" where the merchantBrandName is Netflix, which is again inside a nested json
答案1
得分: 0
- 使用
com.jayway.jsonpath.JsonPath
List<Double> read = com.jayway.jsonpath.JsonPath.read(res, "$.data.content[?(@.element.merchantBrandName == 'Netflix')].absolute");
System.out.println(read.get(0));
// 2170.220006465912
- 使用
io.restassured.path.json.JsonPath
double abs = JsonPath.from(res).getDouble("data.content.find {it.element.merchantBrandName == 'Netflix'}.absolute");
System.out.println(abs);
// 2170.22
// 没时间去搞清楚为什么它被四舍五入了
英文:
There are 2 options for you.
- Using
com.jayway.jsonpath.JsonPath
List<Double> read = com.jayway.jsonpath.JsonPath.read(res, "$.data.content[?(@.element.merchantBrandName == 'Netflix')].absolute");
System.out.println(read.get(0));
//2170.220006465912
- Using
io.restassured.path.json.JsonPath
double abs = JsonPath.from(res).getDouble("data.content.find {it.element.merchantBrandName == 'Netflix'}.absolute");
System.out.println(abs);
//2170.22
//I don't have time to figure out why it's getting rounded
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论