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

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

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

  1. 使用 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
  1. 使用 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.

  1. Using com.jayway.jsonpath.JsonPath
List&lt;Double&gt; read = com.jayway.jsonpath.JsonPath.read(res, &quot;$.data.content[?(@.element.merchantBrandName == &#39;Netflix&#39;)].absolute&quot;);
System.out.println(read.get(0));

//2170.220006465912
  1. Using io.restassured.path.json.JsonPath
double abs = JsonPath.from(res).getDouble(&quot;data.content.find {it.element.merchantBrandName == &#39;Netflix&#39;}.absolute&quot;);
System.out.println(abs);
//2170.22 
//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:

确定