如何使用Rest Assured从API响应中获取值

huangapple go评论97阅读模式
英文:

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 ?

  1. {
  2. "data": {
  3. "content": [
  4. {
  5. "absolute": 1800091.5124980211,
  6. "percentage": 99.9,
  7. "count": 79,
  8. "currency": "USD",
  9. "element": {
  10. "merchantBrandName": "Bliz",
  11. "merchantLogo": "png"
  12. }
  13. },
  14. {
  15. "absolute": 2170.220006465912,
  16. "percentage": 0.1,
  17. "count": 141,
  18. "currency": "USD",
  19. "element": {
  20. "merchantBrandName": "Netflix",
  21. "merchantLogo": "png1"
  22. }
  23. },
  24. {
  25. "absolute": 30.240000128746033,
  26. "percentage": 0.0,
  27. "count": 27,
  28. "currency": "USD",
  29. "element": {
  30. "merchantBrandName": "Wal",
  31. "merchantLogo": "svg"
  32. }
  33. },
  34. {
  35. "absolute": 22.59999990463257,
  36. "percentage": 0.0,
  37. "count": 20,
  38. "currency": "USD",
  39. "element": {
  40. "merchantBrandName": "MAL",
  41. "merchantLogo": "dining.svg"
  42. }
  43. },
  44. {
  45. "absolute": 19.800000429153442,
  46. "percentage": 0.0,
  47. "count": 18,
  48. "currency": "USD",
  49. "element": {
  50. "merchantBrandName": "Ub",
  51. "merchantLogo": "9XrD.png"
  52. }
  53. },
  54. {
  55. "absolute": 15.680000066757202,
  56. "percentage": 0.0,
  57. "count": 14,
  58. "currency": "USD",
  59. "element": {
  60. "merchantBrandName": "way",
  61. "merchantLogo": "iZ.png"
  62. }
  63. }
  64. ],
  65. "page": {
  66. "size": 6,
  67. "totalElements": 6,
  68. "totalPages": 1,
  69. "number": 1
  70. }
  71. }

}

  1. JsonPath js = new JsonPath(response);
  2. 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

  1. 使用 com.jayway.jsonpath.JsonPath
  1. List<Double> read = com.jayway.jsonpath.JsonPath.read(res, "$.data.content[?(@.element.merchantBrandName == 'Netflix')].absolute");
  2. System.out.println(read.get(0));
  3. // 2170.220006465912
  1. 使用 io.restassured.path.json.JsonPath
  1. double abs = JsonPath.from(res).getDouble("data.content.find {it.element.merchantBrandName == 'Netflix'}.absolute");
  2. System.out.println(abs);
  3. // 2170.22
  4. // 没时间去搞清楚为什么它被四舍五入了
英文:

There are 2 options for you.

  1. Using com.jayway.jsonpath.JsonPath
  1. List&lt;Double&gt; read = com.jayway.jsonpath.JsonPath.read(res, &quot;$.data.content[?(@.element.merchantBrandName == &#39;Netflix&#39;)].absolute&quot;);
  2. System.out.println(read.get(0));
  3. //2170.220006465912
  1. Using io.restassured.path.json.JsonPath
  1. double abs = JsonPath.from(res).getDouble(&quot;data.content.find {it.element.merchantBrandName == &#39;Netflix&#39;}.absolute&quot;);
  2. System.out.println(abs);
  3. //2170.22
  4. //I don&#39;t have time to figure out why it&#39;s getting rounded

huangapple
  • 本文由 发表于 2023年3月7日 20:38:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定